Find layers in the current map view using Leafletjs

I have an application that displays recent tasks on the map, like points using Leafletjs.

With Leafletjs, when you want to zoom in to a location discovered by the user, you call something like:

map.locate({'setView' : true, 'timeout' : 10000, maxZoom: 10}); 

However, for some locations, zoom level 10 does not contain any tasks, so I would like to dynamically set the scale so that at least the task is visible to users.

I know that I can listen for the success of the localization function and then check something like:

 map.on('locationfound', function() { //for marker in markers{ //is point within currently visible bounds //break on first positive //else, //zoom up a level, repeat previous checks } } 

but it’s quite inefficient, especially if I have a large number of points.

Does Leaflet have built-in functions / methods for providing layer information in the current map view?

0
source share
1 answer

If you are doing something on the server side, you can do the calculations fast enough.

  • Store locations in pixel coordinates in your database at some increased zoom level (I use zoom level 23). I call this coordinate system "Vast Coordinate System". Then, to get the tile coordinates for a point at a specific location, this is IIRC one bit shift - very fast, and something you can do in SQL.
  • Convert your users location to pixel coordinates in this way - increased level.
  • Iterations at the zoom level. Get the coordinates of the fragment for the user's location at this zoom level, and then execute an SQL query that counts the number of points on this fragment. If> 0, stop.

Your SQL will be something like (sorry, I'm lazy and doing this from memory / thinking, not trying to do it)

 SELECT count(*) WHERE (vcsX>>(zoom+8)==userX>>(zoom+8)) AND (vcsY>>(zoom+8)==userY>>(zoom+8)); 

where vcsX and vcsY are the coordinates of the pixels in the VAST coordinate system.

0
source

All Articles