0

I am trying to draw a path between multiple markers on map. I am defining markers by clicking on map. Means marker are defining on run time by clicking on map. So I want to plot a route between these markers. I don't want shortest path between them. Here is my code:

        function initAutocomplete() {
            var map = new google.maps.Map(document.getElementById('map'), {
                center: { lat: 33.7294, lng: 73.0931 },
                zoom: 13,
                mapTypeId: 'roadmap'
            });
            var latlng1;
            var latlng2;


            // Create the search box and link it to the UI element.
            var input = document.getElementById('pac-input');
            var searchBox = new google.maps.places.SearchBox(input);
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

            // Bias the SearchBox results towards current map's viewport.
            map.addListener('bounds_changed', function () {
                searchBox.setBounds(map.getBounds());
            });

            var markers = [];
            // Listen for the event fired when the user selects a prediction and retrieve
            // more details for that place.
            searchBox.addListener('places_changed', function () {
                var places = searchBox.getPlaces();

                if (places.length == 0) {
                    return;
                }

                // Clear out the old markers.
                markers.forEach(function (marker) {
                    marker.setMap(null);
                });
                markers = [];

                // For each place, get the icon, name and location.
                var bounds = new google.maps.LatLngBounds();
                places.forEach(function (place) {
                    if (!place.geometry) {
                        console.log("Returned place contains no geometry");
                        return;
                    }
                    var icon = {
                        url: place.icon,
                        size: new google.maps.Size(71, 71),
                        origin: new google.maps.Point(0, 0),
                        anchor: new google.maps.Point(17, 34),
                        scaledSize: new google.maps.Size(25, 25)
                    };

                    // Create a marker for each place.
                    markers.push(new google.maps.Marker({
                        map: map,
                        icon: icon,
                        title: place.name,
                        position: place.geometry.location
                    }));

                    if (place.geometry.viewport) {
                        // Only geocodes have viewport.
                        bounds.union(place.geometry.viewport);
                    } else {
                        bounds.extend(place.geometry.location);
                    }
                });
                map.fitBounds(bounds);
            });
            map.addListener('click', function (e) {
                placeMarkerAndPanTo(e.latLng, map);
                map
            });
            function placeMarkerAndPanTo(latLng, map) {
                var marker = new google.maps.Marker({
                    position: latLng,
                    map: map
                });
                map.panTo(latLng);
                var infowindow = new google.maps.InfoWindow({
                    content: '<p>Marker Location:' + marker.getPosition() + '</p>'
                });
                latlng1 = marker.getPosition();
                latlng2 = marker.getPosition();
                google.maps.event.addListener(marker, 'click', function () {
                    infowindow.open(map, marker);
                });
            }

            function mapLocation() {
                var directionsDisplay;
                var directionsService = new google.maps.DirectionsService();
                directionsDisplay = new google.maps.DirectionsRenderer();
                map = new google.maps.Map(document.getElementById('map'), mapOptions);
                directionsDisplay.setMap(map);

                function calcRoute() {
                    var start = new google.maps.LatLng(latlng1);
                    var end = new google.maps.LatLng(latlng2);

                    var bound = new google.maps.LatLngBounds();
                    bound.extend(start);
                    bound.extend(end);
                    map.fitBounds(bound);
                    var request = {
                        origin: start,
                        destination: end,
                        travelMode: google.maps.TravelMode.DRIVING
                    };
                    directionsService.route(request, function (response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(response);
                            directionsDisplay.setMap(map);
                        }
                        else {
                            alert("No path");
                        }
                    });
                }
            }
            mapLocation();   
        }
            google.maps.event.addDomListener(window, 'load', initialize);

strong text

Can someone help me or any suggestion?

hammer
  • 82
  • 10
  • Thanks! Your – hammer May 21 '17 at 08:37
  • 3
    *Here is my code* - you mean you've frankensteined https://developers.google.com/maps/documentation/javascript/examples/places-searchbox and https://developers.google.com/maps/documentation/javascript/examples/event-arguments together without even considering the problems of doing so, and want someone to add more functionality to something that doesn't even function – Jaromanda X May 21 '17 at 09:07
  • Thank you for letting me know this!!! – hammer May 21 '17 at 10:18
  • 1
    related question: [Clickable Driving Direction on Google Maps v3](http://stackoverflow.com/questions/11743799/clickable-driving-direction-on-google-maps-v3) – geocodezip May 21 '17 at 16:06
  • possible duplicate of [Array to create multiple routes on Google Maps v3](http://stackoverflow.com/questions/11778201/array-to-create-multiple-routes-on-google-maps-v3) – geocodezip May 21 '17 at 16:06

0 Answers0