Я создал несколько маршрутов, используя Google Map API.
Мне нужно изменить цвет и выделить маршрут, нажимая на любой из маршрутов на карте Google.
// Добавлен код для нескольких маршрутов.
let directionsService = new google.maps.DirectionsService;
let directionsDisplay;
directionsService.route({
origin: {
lat: sourceLat,
lng: sourceLon
},
destination: {
lat: destLat,
lng: destLon
},
provideRouteAlternatives: true,
travelMode: google.maps.TravelMode['DRIVING']
}, (res, status) => {
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = res.routes.length; i < len; i++) {
directionsDisplay = new google.maps.DirectionsRenderer({
map: this.map,
directions: res,
routeIndex: i,
suppressMarkers: true,
polylineOptions: {
strokeColor: "darkgrey",
strokeOpacity: 1.0,
strokeWeight: 7
}
});
}
}
});
// Добавлен прослушиватель событий для изменения цвета маршрута. Но это не работает, а также не получаю никакой ошибки.
google.maps.event.addListener(directionsDisplay, 'click', function(event) {
directionsDisplay.setOptions({
map: this.map,
directions: res,
routeIndex: 0,
polylineOptions: {
strokeColor: 'green',
strokeOpacity: 1.0,
strokeWeight: 7
}
});
directionsDisplay.setMap(this.map);
});
Заранее спасибо.