我有两个不同的功能,可以从Places服务中检索学校和餐馆。在一个函数中,我将类型作为Schools传递,并获取关于当前位置的学校列表,并使用图标显示在地图上。在另一个功能中,我正在获取众多餐厅。现在问题出在信息窗口上。我单击学校的图标,然后打开信息窗口,然后单击餐厅的图标,但是以前打开的学校图标没有关闭。由于这两个信息窗口都具有两个不同的功能,因此如何关闭所有先前打开的信息窗口。
getSchoolsList() {
var markers = [];
var infoWindow = [];
if (counter == 0) {
let service = new google.maps.places.PlacesService(map);
...
// call placesService and fetch results
...
}, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: './assets/images/marker_inactive.png',
});
markers.push(marker);
// set infowindow for every marker
google.maps.event.addListener(marker, 'mouseover', function () {
if (infowindow != null) {
infowindow.close();
}
google.maps.event.addListener(map, "click", function(event) {
infowindow.close();
});
var index = markers.indexOf(this);
infowindow.setContent('<h1>hi</h1>');
infowindow.open(map, this);
});
}
}
});
});
getParksList() {
var markers = [];
if (parkcounter == 0) {
let service = new google.maps.places.PlacesService(map);
...
// call placesService and fetch results
...
}, (results, status) => {
console.log('results..', results);
this.lengthOfSchools = results.length;
console.log('this.lengthOfSchools', this.lengthOfSchools);
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: './assets/images/marker_active.png',
});
markers.push(marker);
// set infowindow for every marker
google.maps.event.addListener(marker, 'mouseover', function () {
if (infowindow != null) {
infowindow.close();
}
google.maps.event.addListener(map, "click", function(event) {
infowindow.close();
});
var index = markers.indexOf(this);
infowindow.setContent('<h1>hi</h1>');
infowindow.open(map, this);
});
}
}
});
});