kmtgt
kmtgt

Reputation: 33

How can i get coordinates in the route path in google map?

if using the link like this

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 

it will return the value of coordinates as below:

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

but in my javascript, it returned roughly just a few coordinates, 5-6 coordinates

Here is my code to get coordinates :

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

How can i improve my code to get coordinates more specific like the link above? is there any suggestion? :)

Upvotes: 2

Views: 6722

Answers (1)

Roi Reshef
Roi Reshef

Reputation: 51

what you need is not the steps[j].start_point and steps[j].end_point but the path object contained in each step object. that is to say:

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

you want to get each and every point in all of your steps' paths as well, not only the start and end points.

hope that helps

Upvotes: 2

Related Questions