开发者

Google Maps: toggle destinations in directions

开发者 https://www.devze.com 2023-04-03 13:44 出处:网络
I want to toggle my directions with the help of multiple markers in google maps. Its working for single destination. After getting the directions to that single destination, if I click on any other de

I want to toggle my directions with the help of multiple markers in google maps. Its working for single destination. After getting the directions to that single destination, if I click on any other destination marker, it is not updating with new directions. I have the following code in place:

<script type="text/javascript">
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    var home = new google.maps.LatLng(40.703188,-74.005029);

    function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var myOptions = {
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: home
      }
      开发者_如何学Cmap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      directionsDisplay.setMap(map);
    }

    function calcRoute(location) {
      var request = {
          origin: home,
          destination: location.position,
          travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });
    }

    function getNearbyStores() {
        var searchQuery = $('#searchBox').val();
        var request =
        {
            location: home,
            radius: '10000',
            name: [''+searchQuery]
        };  

        var service = new google.maps.places.PlacesService(map);
        service.search( request, callback );

        var marker;
        function callback(results, status)
        {

            if (status == google.maps.places.PlacesServiceStatus.OK)
            {

                var infowindow = new google.maps.InfoWindow();
                var marker, i;

                for ( i = 0; i < results.length; i++ )
                {
                    var place = results[i];

                    var loc = place.geometry.location;
                    var marker = new google.maps.Marker
                    ({
                        position: new google.maps.LatLng(loc.Pa,loc.Qa)
                    });
                    marker.setMap(map);

                    google.maps.event.addListener(marker, 'click', function() {
                        calcRoute(this);
                    });
                }

            }
        }
    }
</script>


I am posting another answer with a running code.

The problem is not in the code that you posted or at least cannot be deduced from it. I do not know how your searchQuery looks like, but you are saying that you get markers for the destinations, then it should be OK. But you might also look at it. I replaced request.name by request.types and added a marker for the home position for a better understanding of the behaviour. Note that your home position is on the water and you want to drive - but suprisingly Google can handle it!

You can test the jsFiddle code in Firefox or Chrome (there are jsFiddle problems in IE).


I see two potential problems:

(1)

position: new google.maps.LatLng(loc.Pa,loc.Qa)
// should preferably be:
position: new google.maps.LatLng(loc.latitude(), loc.longitude())

(2)

google.maps.event.addListener(marker, 'click', function() {                            
     calcRoute(this); 
     // here 'this' refers to the marker, but calcRoute() expects location
     // should probably be:
     calcRoute(this.position);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号