单击地图上的标记后,路线将与infoWindow一起显示。我想在关闭infoWindow时清空我的路线。一旦我点击了地图上的标记,路线将与信息窗口一起显示。我想在关闭infoWindow时清空我的路线。
已尝试directionDisplay.setMap(null)
,但该路由仍未清除。
这是我的听众。我不知道closeClick功能是否正确。
let directionsDisplay = new window['google'].maps.DirectionsRenderer({
suppressMarkers: true,
preserveViewport: true,
});
let directionsService = new window['google'].maps.DirectionsService();
marker.addListener('click', () => {
let infoWindowContent =
'<a href="https://www.google.com/maps/search/?api=1&query=' +
encodeURI(this.camelize(this.hospitals[i].facilityName)) +
'" target="_blank" rel="noopener"><strong>' +
this.camelize(this.hospitals[i].facilityName) +
let infoWindow = new window['google'].maps.InfoWindow({
content: infoWindowContent,
});
infoWindow.open(this.map, marker);
//This is where I suppose to clear the route, during infoWindow closed
marker.addListener(infoWindow, 'closeclick', function() {
directionsDisplay.setMap(null);
directionsDisplay = null;
});
directionsDisplay.setMap(this.map);
if (this.hospitals[i].facilityCode !== this.hospitals[i].leadFacilityCode) {
let request = {
origin: new window['google'].maps.LatLng(
Number(this.hospitals[i].latitude),
Number(this.hospitals[i].longitude)
),
destination: new window['google'].maps.LatLng(
Number(this.hospitals[i].leadFacilityLatitude),
Number(this.hospitals[i].leadFacilityLongitude)
),
travelMode: window['google'].maps.TravelMode.DRIVING,
};
directionsService.route(request, (result: any, status: any) => {
if (status === 'OK') {
directionsDisplay.setDirections(result);
}
});
}
});
我只是添加了这个。
infoWindow.addListener('closeclick', () =>{ directionsDisplay.setMap(null); directionsDisplay.setDirections(null); })