2013-07-16 47 views
2

我可以將響應從谷歌地圖API定向服務以數據庫的形式保存爲JSON,並重復使用它在地圖上使用google.maps.DirectionsRenderer();谷歌地圖v3方向渲染器重繪響應

我已經將它保存在數據庫中,但是當我喜歡使用新的google.maps.DirectionsRenderer()重新繪製地圖時;它不會在地圖上顯示線條。然而,它確實顯示了基於我從數據庫加載的JSON的方向面板。

下面的代碼片段:

  $.ajax({ 
       type: "GET", 
       url: 'controller.php', 
       data: {action: 'routedata', id: id}, 
       dataType: 'json', 
       success: function(data){ 
        if(data && data.routes && data.routes.length > 0){ 
         var thisRouteDirRender = new google.maps.DirectionsRenderer(); 
         thisRouteDirRender.setMap(map); 
         thisRouteDirRender.setPanel(document.getElementById('directions-panel')); 
         thisRouteDirRender.setDirections(data); 
        } 
       } 
      }); 
+0

保存的數據是什麼樣的? – geocodezip

回答

1

我想你可以嘗試somethink這樣的:

$.ajax({ 
    type: "GET", 
    url: 'controller.php', 
    data: {action: 'routedata', id: id}, 
    dataType: 'json', 
    success: function(data){ 
     if(data && data.routes && data.routes.length > 0){ 
      var thisRouteDirRender = new google.maps.DirectionsRenderer(); 
      var directionsService = new google.maps.DirectionsService(); 
      thisRouteDirRender.setMap(map); 
      thisRouteDirRender.setPanel(document.getElementById('directions-panel')); 
      var request = { 
       origin: data.routes[0].LatLng , 
       destination: data.routes[data.routes.length - 1].LatLng, 
       travelMode: google.maps.TravelMode.WALKING 
      }; 
      directionsService.route(request, function(result, status) { 
       if (status == google.maps.DirectionsStatus.OK) { 
        thisRouteDirRender.setDirections(result); 
       } 
      }); 
     } 
    } 
}); 

您需要使用google.maps.DirectionsService建設路線。 也可以從data指定waypoints[]。 您可以在google docs中看到的所有其他參數

+0

我想知道我不能重複使用Google API返回給我的「結果」JSON嗎?實際上,我在代碼中未顯示的初始調用中將該JSON保存在數據庫中。該網站將讓用戶繪製方向並保存它們,然後使用結果JSON重繪它。 – Wikki

+0

您可以嘗試僅存儲座標並使用存儲的座標重繪方向 –

+0

@jonijones如果地圖數據很大,則需要很長時間。我探索了maps.data.addGeoJson()api,但它沒有畫任何東西。 –