简体   繁体   中英

Leaflet markercluster and hashable

I have a map with a lot of markers in cluster, with the markercluster plug ( which i create thanks to a geojson in .JS file as a variable), and i'm trying to be able to dynamically modify the URL hash with zoom/lat/lng with Hashable.js but everytime i'm loading my site with a geojson with more than 40.000 markers, it's seems to have a confict between markercluster and hashable. this is the error i'm getting :

leaflet-src.js:1905 Uncaught TypeError: Cannot read property 'lat' of undefined
at L.LatLngBounds.intersects (leaflet-src.js:1905)
at NewClass._recursively (leaflet.markercluster-src.js:1763)
at NewClass._recursivelyRemoveChildrenFromMap (leaflet.markercluster-src.js:1712)
at NewClass._moveEnd (leaflet.markercluster-src.js:921)
at NewClass.fire (leaflet-src.js:587)
at NewClass.panBy (leaflet-src.js:2575)
at NewClass._tryAnimatedPan (leaflet-src.js:3770)
at NewClass.setView (leaflet-src.js:2460)
at Object.<anonymous> (index.js:85)
at change (hashable.min.js:1)

I suspect the internal Leaflet.markercluster (MCG) state is not fully ready as long as the markers loading is still progressing.

Due to the chunkedLoading option / mode being enabled, this leaves room for hashable to kick in and try to change the map view, whereas your MCG is still in the middle of evaluating your 40k markers. Therefore the map fires some "moveend" event, and MCG tries to display the appropriate markers, but it fails due to missing internal values, hence your error message.

The easy workaround, if acceptable, is to wait for the 40k markers to finish loading, before letting hashable perform its work. For example, you could use a flag that is set to "ready" once addLayers has completed. Within hashable change, check for that flag, and if not ready yet, check some time later (eg using a setTimeout ).

Some plugin improvement possibilities:

Maybe it could be interesting to make sure MCG always has a valid internal state between each chunk of processing, so that the map view can be changed anytime. But that would slow down the overall processing duration obviously.

Another possibility would be to temporarily disable the MCG event listeners on the map, so that it does not try reading its internal variables, while they are being re-processed.

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