简体   繁体   English

Google Map InfoBox反复打开/绘制

[英]Google Map InfoBox repeatedly opening/drawing

We are using Google Maps and I have a case where we have a fully populated map, full of markers and when clicking on a specific result outside of the map, the following code is executed for the corresponding marker. 我们使用的是Google Maps,在这种情况下,我们有一个完全填充的地图,上面充满了标记,当单击地图外部的特定结果时,将为相应的标记执行以下代码。

 map.setCenter(new google.maps.Marker({
    position : new google.maps.LatLng(latitude, longitude)
}).getPosition());
map.setZoom(10);
google.maps.event.trigger(currentMarker, 'click', {latLng: new google.maps.LatLng(latitude, longitude)});

This code is meant to, center the specified marker, zoom in on the marker, and activate the "click" event which in turn draws/opens an InfoBox. 该代码旨在使指定的标记居中,放大该标记并激活“单击”事件,从而依次绘制/打开一个InfoBox。

The problem is that most of the time when this code is run the click event is triggered multiple times on the marker causing the InfoBox to redraw however many times this occurs (it is not a set ammount of times each time). 问题在于,大多数情况下,运行此代码时,会在标记上多次触发click事件,从而导致InfoBox重绘,但发生的次数很多(这不是每次设置的次数)。 Does anyone have an idea why this might be happening? 有谁知道为什么会这样?

I'm quite positive if you're passing the currentMarker into the google.maps.event.trigger you don't need to specify the lat/long. 如果您要将currentMarker传递到google.maps.event.trigger ,则无需指定纬度/经度,我非常肯定。 It should just be google.maps.event.trigger(currentMarker, 'click') . 它应该只是google.maps.event.trigger(currentMarker, 'click')

Likewise another option is using jquery's .on() event on the outside link to detect the click then trigger an infoWindow.open() on the marker. 同样,另一个选择是在外部链接上使用jquery的.on()事件检测点击,然后在标记上触发infoWindow.open() Below is a snippet of how this would look like. 以下是该代码的摘要。

$('#openmarker').on('click', function(e){
    e.preventDefault();
    infowindow.open(map,currentMarker);
    map.setCenter(currentMarker.getPosition());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM