如何在谷歌地图的路径路径中获取坐标?

时间:2011-07-11 16:52:43

标签: google-maps

如果使用这样的链接

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;
        }
      }); 
   }

如何改进我的代码以获得更具体的坐标,如上面的链接? 有什么建议吗? :)

1 个答案:

答案 0 :(得分:2)

你需要的不是步骤[j] .start_point和steps [j] .end_point,而是每个步骤对象中包含的路径对象。也就是说:

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

您希望获得所有步骤中的每一点&#39;路径,不仅是起点和终点。

希望有所帮助