ユーザーからの入力として住所を受け取るアプリケーションがあります。与えられた入力に対して、2 KMS 未満の距離にある (データベースに保存された) アドレスのリストを見つけます。データベースに住所の緯度と経度の情報があります。各アドレスには from&to ポイントがあります。これらの from & to ポイントの Lat&Long もデータベースにあります。
Google マップでこれらの from&to ポイント間に線を引く必要があります。これを行うにはJavascriptを使用します。任意の住所に対して、少なくとも 50 本の線を引く必要があります。OVER_QUERY_LIMIT のため、この応答をデータベースに文字列として保存し、オブジェクトに変換して Google マップに表示したいと考えていました。
私は常に既知のアドレスのリストしか持っていないことに注意してください。これらの住所は、From&To lat&long でデータベースに保存されます。したがって、このように指定された fromlat&long & to-lat&long の応答を取得する cronjob を作成することもできます。
var directionsDisplay<?php print $i; ?> = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay<?php print $i; ?>.setMap(map);
var start<?php print $i; ?> = '<?php echo $address1; ?>';
var end<?php print $i; ?> = '<?php echo $address2; ?>';
var request<?php print $i; ?> = {
origin:start<?php print $i; ?>,
destination:end<?php print $i; ?>,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
<?php if(isempty($routestr)) { ?>
directionsService.route(request<?php print $i; ?>, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var routestr = JSON.stringify(response, null, 4);
//Saving routestr in database using ajax call
directionsDisplay<?php print $i; ?>.setDirections(response);
}
}
<?php } //end of $routestr check
else { //convert $routestr into response and assign to setDirections method
?>
//PHP $routestr is a JSON object automatically. so eval function will not work.
//var response1 = eval('(' + <?php print $routestr; ?> + ')');
var response1 = <?php echo $routestr; ?>; //This works.
alert(response1.status); //displays OK. So I am assured that this is a JSON object now.
directionsDisplay<?php print $i; ?>.setDirections(response1); //This still does not work. Map is blank
<?php }//end of $routestr else check ?>
私は別の部分で問題に直面しています。eval メソッドはオブジェクトを正しく変換しません。したがって、 setDirections(response1) を呼び出すと機能しません。
誰かが助けてくれますか?JSON 文字列を DirectionsResult オブジェクトに変換したい
https://developers.google.com/maps/documentation/javascript/reference#DirectionsResultも確認しました。「この結果は「JSON のようなもの」ですが、LatLng オブジェクトが間接的に含まれているため、厳密には JSON ではないことに注意してください。」
このコードをhtmlファイルでテストしました
var res = new google.maps.DirectionsResult(); //I receive js error This is not a class and cannot be instantiated. Don't understand how they send back this as response in route method call.
res1.status = "OK"; //Becoz of above issue, this will not work
alert(res1.status); //Becoz of above issue, this will not work
しかし、この警告メッセージは決して実行されません。何も表示されません。理由がわからない。
男はこれを試さないでください。これはうまくいきません。
現在、コードを変更し、タイムアウトを実装しました。毎秒最大 5 件のリクエストを directionService に送信できることをどこかで読みました。1 秒間に 5 件のリクエストを超えると、OVER_QUERY_LIMIT が返されます。
タイムアウトを実装すると、最終的な応答が遅くなります。しかし、他に方法はないようです。このサービスをビジネス API として取得することで問題が解決するかどうかはわかりません。