在这个项目中,我有两种不同的标记,我使用以下代码在地图上显示。
for (i = 0; i < beaches.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(beaches[i][1], beaches[i][2]),
map: mapSingle,
icon: icons[beaches[i][3]].icon
});
var infowindow = new google.maps.InfoWindow();
var content = beaches[i][0];
google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(mapSingle,this);
};
})(marker,content,infowindow));
markers.push(marker);
}
现在有了新的要求我必须放置一种可以拖动的新型标记。所以,我有这个代码
com_current = new google.maps.Marker({
map: mapSingle,
draggable: true,
icon: com_Image,
animation: google.maps.Animation.DROP,
position: {lat: parseFloat('20.5937'), lng: parseFloat('78.9629')},
});
google.maps.event.addListener(com_current, 'mouseover', function() {
var info = infowindow.getContent();
infowindow.setContent('show the position of the marker');
infowindow.open(mapSingle, this);
});
现在问题,我得到的是当我悬停在这个可拖动的标记上时,它的信息窗口打开,这是可以的,但其他一些已经打开的infowindow隐藏,反之亦然。即使我将鼠标悬停在这个可拖动的标记中,如何才能创建其他已打开的标记显示。
答案 0 :(得分:1)
因为您使用的是与非可拖动标记相同的信息窗口对象。请尝试使用以下代码。
com_current = new google.maps.Marker({
map: mapSingle,
draggable: true,
icon: com_Image,
animation: google.maps.Animation.DROP,
position: {lat: parseFloat('20.5937'), lng: parseFloat('78.9629')},
});
infoWindowForDraggableMarker = new google.maps.InfoWindow();
google.maps.event.addListener(com_current, 'mouseover', function() {
infoWindowForDraggableMarker.setContent('show the position of the marker');
infoWindowForDraggableMarker.open(mapSingle, this);
});
修改强>
在第一个代码块中,您正在创建信息窗口对象,如下所示进行更改。
var infowindow = new google.maps.InfoWindow();
for (i = 0; i < beaches.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(beaches[i][1], beaches[i][2]),
map: mapSingle,
icon: icons[beaches[i][3]].icon
});
var content = beaches[i][0];
google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(mapSingle,this);
};
})(marker,content,infowindow));
markers.push(marker);
}
并使用相同的对象作为可拖动标记,如下所示。
com_current = new google.maps.Marker({
map: mapSingle,
draggable: true,
icon: com_Image,
animation: google.maps.Animation.DROP,
position: {lat: parseFloat('20.5937'), lng: parseFloat('78.9629')},
});
google.maps.event.addListener(com_current, 'mouseover', function() {
infowindow.setContent('show the position of the marker');
infowindow.open(mapSingle, this);
});