2012-06-27 65 views
0

我有一個應用程序將地址作爲來自用戶的輸入。對於給定的輸入,我在< 2 KMS距離上找到地址列表(保存在數據庫中)。我有數據庫中地址的長信息。每個地址都有一個從&到點。緯&這些從&到這些點也都在數據庫中。保存並重新加載directionService.route響應

現在我需要在這些從&到谷歌地圖中的點之間畫線。我用Javascript來做到這一點。對於任何給定的地址,我將有至少50行被繪製。由於OVER_QUERY_LIMIT,我想以字符串的形式將此響應保存在數據庫中,然後將其轉換回對象並在Google地圖中顯示。

請注意,我總是隻有一個已知地址的列表。這些地址保存在數據庫中,從&到lat &長。所以我也可以寫一個cronjob,它可以檢索給定的響應,就像這樣長的& long & to-lat &。

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「,但它不是嚴格的JSON,因爲它間接包含LatLng對象。」

我在一個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 

測試此代碼,但永遠不會執行此警報消息。沒有顯示。不知道爲什麼。

夥計們不要試試這個。這不會奏效。我的代碼&已執行超時。我讀了一些地方,我們可以每秒向directionService做出5次請求。如果我們在一秒內超過5次請求,OVER_QUERY_LIMIT將被返回。

執行超時減慢了最終響應速度。但似乎沒有其他辦法。不知道是否將此服務作爲業務API來解決問題。

回答

0

我也有這個問題。但在我的情況下,我需要重新繪製該路線並允許用戶拖動路線(與我們提出路線請求時存在的屬性相同)並更新BD中的經緯度。如果你只需要畫出你已經擁有的這兩點之間的界限,那麼你可以申請一條新的路線。如果您有Over_query_limit問題,您可以保存response.routes [0] .overview_path中的LatLng,並用簡單的Polyline將LatLng解析爲路徑將其拉回。當然這不會是一個新的路線請求。

+0

我嘗試這樣做: directionsService.route(請求,功能(響應,狀態){ 如果(狀態== google.maps.DirectionsStatus.OK){ VAR STR = JSON.stringify(響應); \t var obj = JSON.parse(str); \t directionsDisplay。setDirections(STR); } }); 和瀏覽器返回屬性中的錯誤:(該值不是數組)。第1行main.js 所以我認爲該對象在JSON.parse(str)中被損壞。 我甚至不知道是否允許保存Google數據。 – Daniel

+0

我已經嘗試繪製折線。但這不是正確的解決方案,因爲有時街道是L形的,並持續不斷。繪製多段線只是一個疊加線,不會與地圖上的街道線同步。 –

+0

我已經試過了。你應該試試它作爲directionsDisplay.setDirections(obj);但是這不起作用,因爲DirectionsResult(obj)只是一個類JSON對象而不是完全的JSON。所以stringfying和解析回來並沒有真正的幫助。 –