我有一张带有一堆标记的 map ,以及对应于每个标记的数据表。当用户单击表中的项目时,将打开相应标记的 InfoWindow
。本地图缩小并且标记全部可见时一切正常,但如果 map 放大,并且通过单击表中的项目打开屏幕外标记的 InfoWindow
,这是发生了什么:
- map 滚动到正确的位置,
InfoWindow
已经打开 - map 停止平移,
InfoWindow
消失。
关于可能发生的事情以及如何解决这个问题有什么建议吗?
最佳答案
好吧,这个问题与我使用的 Marker Clusterer 有关在 map 上...本质上,发生了以下情况:
- 单击表格中的项目,
InfoWindow
打开 - map 平移到显示
InfoWindow
的位置
- 平移完成后,Marker Clusterer 会重新绘制(如果需要),并强制关闭
InfoWindow
。
我的解决方案是,当点击表中的一个项目时,我得到相应的 Marker's
latlng,手动平移到这个位置,等待平移通过 'idle' 监听器完成,然后完成后(Clusterer 已重新绘制),然后我打开 InfoWindow
。
// get map, marker positions
var mapLatLng = GLOBAL_map.getCenter();
var markerLatLng = GLOBAL_markers[index].getPosition();
// pan the map
if(!markerLatLng.equals(mapLatLng)) {
// map will need to pan
GLOBAL_map.panTo(markerLatLng);
google.maps.event.addListenerOnce(GLOBAL_map, 'idle', function() {
// open InfoWindow
GLOBAL_infowindow.setContent(GLOBAL_markers_content[index]);
GLOBAL_infowindow.open(GLOBAL_map, GLOBAL_markers[index]);
});
} else {
// map won't be panning, which wouldn't trigger 'idle' listener
GLOBAL_infowindow.setContent(GLOBAL_markers_content[index]);
GLOBAL_infowindow.open(GLOBAL_map, GLOBAL_markers[index]);
}
关于google-maps-api-3 - 谷歌地图 : InfoWindow closing after auto pan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907145/