2

Is there any way to get the geometry line from google maps where you ask for a route?

I would want to get the blue line in this image, in order to get some of them programatically.

I know that I can get transit information from goole API, but I do not know if there is any way to get the geometry.

enter image description here

1

Response from the directionsService will give you an array of points in overview_path in each route. Following is the code sample for better understanding

       directionsService.route({
            origin: routeFromMarker.getPosition(),
            destination: routeToMarker.getPosition(),
            avoidTolls: false,
            avoidHighways: false,
            travelMode: google.maps.TravelMode.DRIVING
        }, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
                var myRoute = response.routes[0].overview_path;
                for (var i = 0; i < myRoute.length; i++) {
                    routeMarkers[i] = myRoute[i];
                }
                var linePath = [];
                $.each(routeMarkers,function(key,location){
                    linePath.push({lat: parseFloat(location.lat()), lng: parseFloat(location.lng())});
                });
                routeLine = new google.maps.Polyline({
                    path: linePath,
                    strokeColor: "#f45017",
                    strokeOpacity: 1.0,
                    strokeWeight: 3,
                    map: map
                });  
            } else {
                window.alert('Directions request failed due to ' + status);
            }
        });

You can use this points to make a polyline and use its geometry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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