Я пытаюсь установить infoWindows для маркеров, которые создаются в функции обратного вызова службы DirectionsService (Google Maps API V3).Я безуспешно пытался сделать это разными способами.
Вот скелет того, как выглядит мой код:
for(i=0;i<markersList.length;i++){
map = someMap
var request = myRequestObject;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
directionsService.route(request, (function (address) {
return function(response, status) {
//Use the directions service to do some stuff
//..Then create markers
var marker = new google.maps.Marker({
map: map,
title: address['title']
});
//push the marker to a list of markers attached to the map
//this can be useful to delete all markers in a later step
map.markers.push(marker);
//Creating multiple instances of infoWindows works fine though..
//However, Creating 1 info window for the whole map doesn't work from here
//If I try that, I'd set the same content for all info windows
//which is not what I want.
}
}
)(markersList[i])
);
}
Я пытался сделать это, но и это не помогло:
//add setInfo as a custom function to my map
//then call it to iterate over all markers and add info windows
google.maps.Map.prototype.setInfo = function() {
for(var i=0; i < this.markers.length; i++){
infowindow.setContent(this.markers[i].getTitle());
google.maps.event.addListener(this.markers[i], 'click', function(){
infowindow.close();
infowindow.open(map,this.markers[i]);
});
}
};
Затем я вызову эту функцию после того, как я закончу с directionsService.route (проверьте первый блок кода), вне моего основного цикла for.Однако по какой-то причине он никогда не находит никаких маркеров, прикрепленных к карте.
Есть мысли о том, как назначить нужные информационные окна для маркеров карты?Я хочу иметь 1 экземпляр infoWindow, чтобы можно было закрыть его при нажатии нового окна infoWindow (infoWindow.close ()).
Спасибо!