2017-11-06 6 views
1

Google Maps APIを使用して、20の場所のリストとそこに到達する時間を表示しています。私はgoogle.maps.DirectionsServiceを使用しています。このAPIには、1秒あたりの制限数が10通に設定されています。すべての電話を同時に呼び出すことができます(Testedと完全に動作します)。私が10以上の場所を持っているとき、私は呼び出しブロックを使って呼び出しています。 16の拠点を持つ例えば:私は、私は3秒を待って、私は10あたりの制限を尊重していますので、理論的には、このアプローチは動作するようになって他人のために6Google Maps API - 1秒あたりのクエリリミット超過

を呼び出して、最初の10点の位置を求めます秒。 しかし、動作しません。 OVER_QUERY_LIMITというエラーが表示され、残りの6か所を呼び出すときにが表示され、時には4つが失敗し、3つが失敗することがあります。待機時間を5秒に増やすと、時にはうまくいくこともありますが、時にはその1つが失敗することもあります。

質問:この制限は毎秒10クエリではありませんか?はいの場合、私は何が間違っていますか?どのように私はそれを修正することができますか?

//------------------ Code ---------------- 

getDistanceByBlocks(destinations: IDestination[]){ 
    let limit = 8; 
    if(destinations.length <= limit){ 
     destinations.forEach(destination => { 
      this.getDistance(destination); 
     }); 
    }else{ 
     let selection = destinations.slice(0, limit) 
     let rest = destinations.slice(limit) 
     selection.forEach(destination => { 
      this.getDistance(destination); 
     }); 
     setTimeout(() => { 
      this.getDistanceByBlocks(rest) 
     },3000); 
    } 
} 

getDistance(destination: IDestination) { 
    if (this.dataService.getCurrentLocation()) { 
     let mapSettings = this.userSettings.getMapSettingsSync(); 
     this.mapService.getRoutes(destination, mapSettings); 
    } 
} 

//------------------ MapService---------------- 

getRoutes(destination: IDestination, mapSettings:IMapSettings): void { 
    let directionsService = new google.maps.DirectionsService; 
    this.currentLocation = this.dataService.getCurrentLocation(); 

    directionsService.route(
    { 
     origin: { lat: this.currentLocation.latitude, 
     lng: this.currentLocation.longitude }, 
     destination: { lat: destination.latitude, lng: destination.longitude }, 
     provideRouteAlternatives: mapSettings.provideRouteAlternatives, 
     avoidHighways: mapSettings.avoidHighways, 
     avoidTolls: mapSettings.avoidTolls, 
     drivingOptions: { 
      departureTime: new Date(Date.now()), 
      trafficModel: mapSettings.trafficModel 
     }, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING, 
     unitSystem: google.maps.UnitSystem.IMPERIAL 
    }, 
    (response, status) => { 
     if (status == google.maps.DirectionsStatus.OK) { 
      let routes: IRouteInfo[] = response.routes 
       .map(route => { 
        let routeInfo: IRouteInfo = { 
         summary: route.summary, 
         distance: route.legs[0].distance.text, 
         timeValue: route.legs[0].duration.text, 
        }; 
        return routeInfo; 
       }); 
      destination.routes = routes; 
      this.events.publish(AppSettings.UPDATE_DESTINATIONS) 

     } else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT){ 
      console.warn(status); 
     } else { 
      console.warn(status); 
     } 
    }); 
} 

クロームネットワーク]タブでは、我々はそれぞれの呼び出しとどのように多くのエラー、私が経験しています間の待ち時間を見ることができることに注意することが重要です。私が興味をそそられるのは、失敗した電話が見えないということです。クロムは私にそれを見せません。だから私はgoogle.mapsサービスはJavaScriptでそれを処理していると思う、それはサーバーへの呼び出しを行っていないが、実際に私は手掛かりを持っていない。

Chrome Network Tab Example

+0

コードでGoogleマップJavascript API v3キーを使用していますか? – geocodezip

+0

リクエストの間に追加の遅延がありますか?それはすぐに8リクエストを発射し、残りの3秒後に発砲するように見えます。どこから1秒あたり10回のクエリのクエリレートを取得しましたか? – geocodezip

+0

こんにちはgeocodezip、この方法でG Maps APIキーを使用しています。 "" html – AMore

答えて

1

私は私のプロジェクトの一つで同じ問題を抱えていました。私はあなたが各要求の間に時間をとらなければならないと思います。このように、サーバーは継続的に料金が請求されます。私のプロジェクトでは、次のコードを使用しています。

for (var i = 0; i<6; i++) { 
    (function (i) { 
    setTimeout(function() { 
     var service = new google.maps.places.PlacesService(map); 
     var request = { 
      placeId: yourArray [i] 
     }; 

     service.getDetails(request, callback); 
    }, i * 300); 
    })(i); 
} 

ここでリクエストを定義して送信します。各リクエストの間に0.3秒の時間を与えることに注意してください。あなたはOVER_QUERY_LIMITエラーがある場合

そして、あなたも知っているために、このコードを使用することができます:

function callback (place, status) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
     ... 
    } else if (status != google.maps.places.PlacesServiceStatus.OK && status != google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) { 
     console.error("Fail get detail try to rerequest.:("); 
     ... 
     // For specials errors. 
    } else if (status == google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) { 
     console.error("Over Query Limit"); 
     retry(); 
    } 

} 

あなたには、いくつかの質問や、いくつかのコメントがあれば教えてください。

+0

**助けてくれてありがとうございます** **すでにこのオプションを試しましたが、各リクエストの間に遅延を入れても機能しませんでしたので、もう一度やり直してみてください。 – AMore

関連する問題