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.

getBounds()/fitBounds() + lots of markers = renders the map grey/unusable

See original GitHub issue

Hello,

Intro

First thank you for the awesome plugin, we use it at quite some projects over here. 😃

At one of our projects we make use of the marker cluster plugin (the markercluster plugin is not the problem, read below):

marker[markersData[i][0]] = L.marker([_lat, _lng], {icon: icon});
markers.addLayer(marker[markersData[i][0]]);

Then we use:

var bounds = markers.getBounds(); // [1]
map.fitBounds(bounds); // [2]
map.addLayer(markers);

Problem

Okay this usually works perfectly fine until we have a case where we want to spawn lots of markers (2000-5000 markers). For some reason, doing this renders a grey map and it’s unusable. The map works just fine with less than 500 markers.

By unusable I mean the following: the map can’t be dragged with the mouse, nor scroll-zoomed, the zoom buttons don’t work, etc… I do can see the markers, but neither the popups open or anything.

With the .fitBounds() [2] code in (from the above code), the map gets rendered like this: leaflet-not-ok

What we’ve tried

If I remove [2] (from the above code) the maps renders OK (but highly unzoomed which is what we don’t want). leaflet-ok

The .getBounds() function returns coherent data:

o.LatLngBounds
    _northEast: o.LatLng
        lat: 42.0468718
        lng: 2.7438793000000032
    _southWest: o.LatLng
        lat: 41.219172
        lng: 1.6023217

We know the problem doesn’t come from the markercluster because we’ve tried removing it and the same issue happens. leaflet-not-ok-2

So we’re out of ideas how we might fix this by ourselves. We hope you can help us 😃.

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mrAceTcommented, Sep 20, 2018

A bit of and old issue, but I had strange grouping problems with just a dozen markers. Without the call to fitBounds everything worked fine. But the fitBounds call really messed things up.

After a lot of searching without any really good solution I had a great idea (if I may say so 😉: First initialize the map with zoomlevel 1 (or 2) (zooming out as far as you can) Then you have all your markers bundled in just one group, then in stead of calling the fitBounds function, you do: $(‘#’+container.id+’ .marker-cluster’).first().click();

Et voila ! You use the “bult in fitBounds” of the markerCluster plugin!

PS: assuming you you are using jquery

1reaction
badrealcommented, Feb 4, 2016

adding a timeout before calling fitbounds might be a solution , it was in my case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

leaflet - How to fit bounds after adding multiple markers
I am using this piece of code to display map markers pulled from a MySQL database using leaflet js on an open ...
Read more >
Mapbox GL JS getBounds()/fitBounds() - Stack Overflow
So I know how to fitBounds, but I'm unsure how to get them map.getBounds() just seems to return the set centre position lng/lat....
Read more >
Leaflet fitbounds. Enable here. js to help visualize a route on a ...
AJAX () 0 Geting fitBounds and removeLayers to work in Leaflet getBounds ()/fitBounds () + lots of markers = renders the map grey/unusable...
Read more >
Differences between fitBounds and getBounds - Google Groups
Hi! I have a problem with the two methods fitBounds and getBounds ... Look at this example : var map = new google.maps.Map(document.getElementById('map'),...
Read more >
Documentation - a JavaScript library for interactive maps
keyboard, Boolean, true, Makes the map focusable and allows users to ... getBounds(), LatLngBounds ... tileload, TileEvent, Fired when a tile loads.
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