Google Map API: Draw single route line on Map from start location to end location

Last Reply 10 months ago By kalpesh

Posted 10 months ago

Hello All, Good Morning!! I have facing trouble to show the perfect google map in may appliation while draw route between locations. I am serching following location on the map,and draw the route between them, 1)ambavadi 2)Maninagar 3)gujarat university 4)Motera

https://ibb.co/keHFkH

While i am search it on maps.google.com,then it will show the folloing path. https://ibb.co/m5PvkH

But when i am searching it in my asp.net website then it shows me correct route but it also shows extra straight line between locations which i dont want please see following image for that, https://ibb.co/jWQ1QH

I am using following code for that to display map and draw the route.

 

 

<script type="text/javascript">
                                        function map1() {
                                            var markers = [
<asp:Repeater ID="rptMarkers" runat="server">
<ItemTemplate>
            {
            "title": '<%# Eval("LocationName") %>',
            "lat": '<%# Eval("Latitude") %>',
            "lng": '<%# Eval("Longitude") %>'
           
        }
</ItemTemplate>
<SeparatorTemplate>
    ,
</SeparatorTemplate>
</asp:Repeater>
];
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var infoWindow = new google.maps.InfoWindow();
        var lat_lng = new Array();
        var latlngbounds = new google.maps.LatLngBounds();
        for (i = 0; i < markers.length; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            lat_lng.push(myLatlng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            });
            latlngbounds.extend(marker.position);
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
        map.setCenter(latlngbounds.getCenter());
        map.fitBounds(latlngbounds);
 
        //***********ROUTING****************//
 
        //Initialize the Path Array
        var path = new google.maps.MVCArray();
 
        //Initialize the Direction Service
        var service = new google.maps.DirectionsService();
 
        //Set the Path Stroke Color
        var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7' });
 
        //Loop and Draw Path Route between the Points on MAP
        for (var i = 0; i < lat_lng.length; i++) {
            if ((i + 1) < lat_lng.length) {
                var src = lat_lng[i];
                var des = lat_lng[i + 1];
                path.push(src);
                poly.setPath(path);
                service.route({
                    origin: src,
                    destination: des,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                }, function (result, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                            path.push(result.routes[0].overview_path[i]);
                        }
                    }
                });
            }
        }
    }
</script>

What can i suppose to do?please suggest.

Thanks, Rucha

You are viewing reply posted by: kalpesh 10 months ago.
Posted 10 months ago

Don't push the src point on to the path out of order (the directions service is asynchronous).

Remove this line:

path.push(src);

And change the code to render each directions result as a separate polyline.

And change the script for ROUTING like below.

//***********ROUTING****************//

//Initialize the Direction Service
var service = new google.maps.DirectionsService();

//Loop and Draw Path Route between the Points on MAP
for (var i = 0; i < lat_lng.length; i++) {
    if ((i + 1) < lat_lng.length) {
      var src = lat_lng[i];
      var des = lat_lng[i + 1];         
      service.route({
        origin: src,
        destination: des,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
        }, function (result, status) {
            if (status == google.maps.DirectionsStatus.OK) {            
            //Initialize the Path Array
			var path = new google.maps.MVCArray();
			//Set the Path Stroke Color
			var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7' });
			
			poly.setPath(path);
			
                for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                    path.push(result.routes[0].overview_path[i]);
                }
            }
        });
    }
}

Refer

draw different route line between multiple markers maps