简体   繁体   中英

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.

 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.

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). 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. It should just be 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. 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());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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