question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’m posting this partly for confirmation that I’ve done it right and partly in case anyone else may need it in future or even if it is something that could/should be added to leaflet.

I was having trouble with moveend or dragend zoomend calling the results function too soon which prevented the user from being able to do more than a single move (drag or zoom) at once (unless they were quick) before it would want to run the function so, taking inspiration from google maps, I’ve tried to replicate the idea of the idle event. Rather than run the function directly on moveend or dragend zoomend it allows me to wait for user interaction to stop (500ms since last movement in this case) before running the function.

// declare timeout function name
var timeoutHandler;
...
// initialize map as normal
var map = L.map('map_canvas', {
  ...
});
// add event listener to map for movement
map.on('zoomend dragend', mapMoveHandler);
map.on('idle', mapIdleHandler);
...
function mapMoveHandler() {
  // cancel any timeout currently running
  window.clearTimeout(timeoutHandler);
  // create new timeout to fire idle event after 500ms (or whatever you like)
  timeoutHandler = window.setTimeout(function() {
    map.fire('idle');
  }, 500);
}
...
function mapIdleHandler() {
  // run some function when map idle to get results or update markers or something
  ...
}

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:3
  • Comments:41 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wololodevcommented, Nov 5, 2016

with lodash.js, this is an exact replacement:

map.on('moveend', _.debounce(function() {
   console.log("waow");
}, 1000));
0reactions
amnesia7commented, Jan 11, 2016

@GertS I found that for best results I had to cover all possible drag (and zoom in my case) listeners dragstart drag dragend zoomstart zoomanim zoomend for my mapMoveHandler function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events | Maps JavaScript API - Google Developers
Each Maps JavaScript API object exports a number of named events. Programs interested in certain events will register JavaScript event listeners for those ......
Read more >
idle event on Google-maps with jQuery - Stack Overflow
For others having this issue: You can get the map object/variable like this: $('#map_canvas').gmap(); var map = $('#map_canvas').gmap('get', 'map');.
Read more >
Add Idle Event Listener on Maps - techstrikers.com
In this example you will learn how to add Idle Event Listener to google map. This event fires when the map becomes idle...
Read more >
Must wait for 'idle' event on map before calling ... - GitHub
I've this error code : "uncaught exception: Must wait for 'idle' event on map before calling markersNearAnyOtherMarker". I don't understand the ...
Read more >
"idle" event fires too soon - Google Groups
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group. To post to this group,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found