Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have this code:

    var directionsService = new google.maps.DirectionsService();

    var request = {
      origin : start,
            destination : end,
            travelMode : google.maps.DirectionsTravelMode.DRIVING
    };

directionsService.route(request, function(response, status) {
    var coords;
    if (status == google.maps.DirectionsStatus.OK) {
        coords = google.maps.geometry.encoding.decodePath(response.routes[0].overview_polyline.points);
        callback(coords);
    }
});

Yesterday the decodePath brought coordinates whose name of variables was Ya and Za, today became hb and ib. Why?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You shouldn't access the coordinates using the property names (i.e. 'Ya' and 'Za') instead use the .lat() and .lng() methods of the google.maps.LatLng object.

So I assume your callback function loops through the array of the coordinates (as variable 'coords'). The simple example below would loop through this array and print out each coordinate to the console in the fashion yy.yyyyy,-xx.xxxx

function callback(coords){
    var i;
    for(i=0;i<coords.length;i++){
        console.log(coords[i].lat()+','+coords[i].lng())
    }
}

This code assumes that the variable 'coords' is the overview_path property of the DirectionsRoute object (part of the DirectionsResult object.). This should be what you are getting by using the decodePath() function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.