2011-07-11 59 views
2

如果使用這樣我怎樣才能獲得在谷歌地圖路線路徑的座標?

http://maps.google.com/maps?f=d&hl=en&saddr=25.04202,121.534761 
&daddr=25.05202,121.554761&ie=UTF8&0&om=0&output=kml 

鏈接它會返回座標值,如下:

121.534760,25.042040,0.000000 121.533650,25.042190,0.000000 121.532950,25.042430,0.000000 121.532950,25.042430 ,0.000000 121.532980,25.044400,0.000000 121.532980,25.044400,0.000000 121.534000,25.044670,0.000000 121.534400,25.044820,0.000000 121.535690,25.045690,0.000000 121.536400,25.045990,0.000000 121.540510,25.046700,0.000000 121.543860,25.047490,0.000000 121.546430,25.048160,0.000000 121.549130,25.048140 ,0.000000 121.549130,25.048140 ,0.000000 121.550770,25.048110,0.000000 121.552570,25.048140,0.000000 121.552570,25.048140,0.000000 121.552700,25.049850,0.000000 121.552750,25.051440,0.000000 121.552650,25.051540,0.000000 121.552640,25.051680,0.000000 121.552640,25.051680,0.000000 121.552660,25.052280,0.000000 121.552660,25.052280 ,0.000000 121.553770,25.052260,0.000000 121.553770,25.052260,0.000000 121.554770,25.052240,0.000000

但在我的JavaScript,它返回大致短短座標,座標5-6

這裏是我的代碼來獲得座標:

function route(FromPlace,ToPlace){ 
if(!FromPlace&&!ToPlace){ 
    FromPlace = new google.maps.LatLng(13.692941, 100.750723); 
    ToPlace = document.getElementById("endPoint").value; 
} 
    //directionsService = new google.maps.DirectionsService(); 
    var request = { 
      origin: FromPlace, 
      destination: ToPlace, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
     }; 
    directionsService.route(request, function(response, status) { 
     if (status == google.maps.DirectionsStatus.OK) 
     { 

      directionsDisplay.setDirections(response); 
    for (var i = 0; i < response.routes[0].legs.length; i++) { 
      for (var j = 0; j < response.routes[0].legs[i].steps.length; j++) { 
       if(j == response.routes[0].legs[i].steps.length-1){ 

        addressRoute = addressRoute+response.routes[0].legs[i].steps[j].end_point.lat()+"," 
         +response.routes[0].legs[i].steps[j].end_point.lng()+""; 
        latitude = response.routes[0].legs[i].steps[j].end_point.lat(); 
        longitude = response.routes[0].legs[i].steps[j].end_point.lng(); 
        destinationDB = latitude +","+ longitude; 
        }else{ 

        addressRoute = addressRoute+response.routes[0].legs[i].steps[j].end_point.lat()+"," 
         +response.routes[0].legs[i].steps[j].end_point.lng()+"|"; 

        } 
       } 
      } 
      //document.getElementById("textLocation").innerHTML = addressRoute; 
     } 
     }); 
    } 

我該如何改進我的代碼才能獲得更像上面鏈接的座標? 有什麼建議嗎? :)

+0

您是否要求更高的精度?也就是說,度:分:秒。 – thegrinner

回答

2

你需要的不是步驟[j] .start_point和步驟[j] .end_point,而是包含在每個步驟對象中的路徑對象。也就是說:

route.legs.forEach(function(leg){ 
    leg.steps.forEach(function(step){ 
     step.path.forEach(function(point){ 
      debug_panel.innerHTML+=point+"<br/>"; 
     }); 
    }); 
}); 

您希望獲得所有步驟路徑中的每個點,而不僅僅是開始點和結束點。

希望可以幫助

相關問題