stop idle event in Google Maps API v3
I'm using a Google map with markers setup to show locations on a map. I'm using the idle
event to determine when to submit the request for new data, so that when a user pans or zooms the map, an ajax service will be called and the markers will be updated to find locations in the new viewport.
The problem is that when a user clicks on a marker to pop up the infowindow, the map often moves slightly to center the infowindow-- which triggers another idle
event and resets the map, closing the infowindow.
Is there a best practice for this? Is there a way to disable the idle
listener when an infowindow is opened, or is there a standard workaround?
I had similar issue with my googleMaps and here was my workaround....
During my call for Markers, I downloaded points beyond the viewport. For example.... If my N bound lat was 85 and my S bound was 88, I would pull points between 83 and 90 latitude.
viewportN = 85;
viewportS = 88;
searchBoundN = 83;
searchBoundS = 90;
When you click on the marker, it auto-pans you so that your Northbound is now 84. You put an if statement in your idle function that says:
map.addListener("idle", function () {
//get map viewport bounds
.....
// check if you're in territory that doesn't have markers
if (viewPortN < searchBoundN || viewportS > searchBoundS || <<repeat for E, W>>) {
refreshMarkers();
}
});
This should solve your problem. It tells your script to only refresh the markers after a large move. I have my map pulling markers from twice the area of the viewport, just to be safe. It also keeps your data from refreshing from minor pans, which will lower the number of requests to your server.
I had the same problem and because I don't have too much experience in Front-end development I tried to enable actual search and populating markers if only the event comes from user interaction like 'drag' or 'zoom-changed' event.
At first I looked for a way to find out is there any builtin property or flag within idle event and it was none. And then I made one for myself.
Because those events get fire before 'idle' event I set a flag in those events( I know it is not best practice) and in 'idle' event I checked if the 'isUserInteraction' flag is true the process of populating markers will work.
链接地址: http://www.djcxy.com/p/81550.html