如何通过单击地图在多个标记之间绘制路线

时间:2017-05-21 08:12:21

标签: javascript google-maps

我正在尝试在地图上绘制多个标记之间的路径。我通过点击地图来定义标记。通过单击地图在运行时定义平均值标记。所以我想绘制这些标记之间的路线。我不想要它们之间的最短路径。 这是我的代码:



        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);
&#13;
&#13;
&#13;

强文

有人可以帮助我或任何建议吗?

0 个答案:

没有答案