Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a global database of objects (points on a map) that I would like to show on a Leaflet map. Instead of loading all the database (simply too big) at once and creating the objects on a Leaflet LayerGroup, is there a more efficient way to go about querying data, perhaps as each map tile loads, or am I looking at creating a custom solution for this?

share|improve this question
    
How do you serve the map? I'd expect the server to have a method that can be queried using a bounding box. Or depending on the amount of objects, you can perhaps use markerclusters. Their example has a map displaying 50000 items. –  flup Mar 24 '13 at 10:24
    
I do not want to use markerclusters, as I want to display every marker which is visible instead of aggregating. What I am trying to decide is exactly how I should serve my map. As this is moved or zoomed, it would probably not make much sense to query the server every time according to the new bounding box (which may partially overlap the old one). I can image a few ways to do this, but before I reinvent the wheel, I would like to make sure there is not a good solution to this already –  CuNimb Mar 24 '13 at 22:43
    
Well, at some point, when you zoom out to the entire world, that'd mean showing every single marker. So then clustering does make sense. Or will you show no markers at all below some minimum zoom level? How many markers do you have? –  flup Mar 24 '13 at 22:51
    
No markers below a zoom level. Markers are linked to geographical detail (which is the reason I do not want clustering). After a certain zoom they loose their meaning. –  CuNimb Mar 26 '13 at 9:25
1  
Currently, you need custom solution, but you can limit your code by using a governed standard such as Web Feature Service (en.wikipedia.org/wiki/Web_Feature_Service). You can serve up your points as a WFS, which will allow you to add tons of query parameters, such as limiting your results by bounding box, zoom level, etc. You can use free software to host this service (like GeoServer) A typical WFS would return the data in GeoJSON format. You can then use a Leaflet GeoJSON layer to display the data - one such WFS Leaflet plugin here: github.com/azgs/azgs-leaflet. –  Patrick D Mar 26 '13 at 17:14
add comment

1 Answer

up vote 8 down vote accepted

You could watch the 'moveend' event on map with map.on('moveend',loadstuff), make an ajax call inside loadstuff() to grab markers inside the current map.getBounds(), and then add/remove markers(I would assume you have some sort of global identifier for each of them) whether they are inside or outside the current view.

share|improve this answer
    
This should be marked as an answer, it's the proper way to do it. –  Rudi Apr 23 '13 at 19:57
    
Thanks for indicating this. I am not a very sophisticated SO user yet –  CuNimb Feb 26 at 9:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.