Sunday 15 September 2013

google maps - Add stopover to waypoints -



google maps - Add stopover to waypoints -

i having problem adding stopover points google maps route. calcroute function below. have array of latlng objects called waypts , points between waypts[0] , waypts[waypts.length-1] should stopover points. know need loop through points , create stopover this:

<script type="text/javascript"> var directionsdisplay; var directionsservice = new google.maps.directionsservice(); var map; var phpway = <?php echo json_encode($phpway); ?>; var waypts = []; for(var = 0; < phpway.length; i+=2){ waypts.push(new google.maps.latlng(phpway[i], phpway[i+1])); } function initialize() { directionsdisplay = new google.maps.directionsrenderer(); var chicago = new google.maps.latlng(41.850033, -87.6500523); var mapoptions = { zoom: 6, center: chicago } map = new google.maps.map(document.getelementbyid('map_canvas'), mapoptions); directionsdisplay.setmap(map); } function calcroute() { var start = waypts[0]; var end = waypts[waypts.length-1]; var mywaypts = []; (var = 1; < waypts.length-1; i++) { mywaypts.push({ location:waypts[i], stopover:true}); } var request = { origin: start, destination: end, waypoints: mywaypts, optimizewaypoints: true, travelmode: google.maps.travelmode.driving }; directionsservice.route(request, function(response, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); var route = response.routes[0]; } }); } google.maps.event.adddomlistener(window, 'load', initialize); </script>

you should not include start/end points in waypoints array, , pass waypts array in request way:

var request = { origin : start, destination : end, waypoints: waypts, travelmode : google.maps.travelmode.driving };

see doc: https://developers.google.com/maps/documentation/javascript/directions?hl=fr#waypoints

google-maps google-maps-api-3

No comments:

Post a Comment