API do Google Maps: várias direções / rotas no mesmo mapa
Eu tenho um problema ao exibir várias rotas no mesmo mapa do Google.
Tenho uma lista de posições que recebo do meu controlador (neste formulário
(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
arriveeLat: 48.784
arriveeLng: 2.40735
departLat: 48.9016
departLng: 2.29873
Gostaria de fazer com que todas as rotas sejam exibidas no mesmo mapa.tualmente, apenas um é exibido (provavelmente o últim
var map;
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
directionsDisplay.setMap(map);
var listPos = <?php echo json_encode($listPos); ?>;
for (var i = 0; i < listPos.length; i++) {
var startPoint = new google.maps.LatLng(listPos[i]['departLat'], listPos[i]['departLng']);
var endPoint = new google.maps.LatLng(listPos[i]['arriveeLat'], listPos[i]['arriveeLng']);
calculateAndDisplayRoute(directionsService, directionsDisplay, startPoint, endPoint);
}
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, startPoint, endPoint) {
directionsService.route({
origin: startPoint,
destination: endPoint,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Impossible d afficher la route ' + status);
}
});
}