2

I am plotting the lines from geojson file on a leaflet map. Now I need to read the fetaures back which exist within the map bounds based on current view.

I cam across this link: https://github.com/stefanocudini/leaflet-list-markers/blob/master/src/leaflet-list-markers.js which basically checks if the markers exist within the bounds using layer.getLatLng() function. Tried using this but it throws "method does not exist" in my case.

Is there anyway to check if the line feature exists within map bounds after they have been added on the map.

Nitesh Rai
  • 35
  • 1
  • 6

1 Answers1

4

You can check whether the line's bounding box overlaps the map's bounding box:

if (map.getBounds().intersects( line.getBounds() )) { ... }

Keep in mind that this checks the bounding boxes. If you need more accurate intersection calculations, you might want to use TurfJS's intersect method.

You say that you can not run getLatLng() on a L.Polyline, and this is not a surprise. Read the Leaflet documentation carefully, and you'll see that markers have a getLatLng() method, and polylines have getLatLngs() and getBounds() instead.

IvanSanchez
  • 18,272
  • 3
  • 30
  • 45
  • Thanks @Ivan. Intersects works for me. It is not very accurate as you mentioned but definitely a good start. – Nitesh Rai Dec 09 '16 at 03:30
  • @IvanSanchez turf intersect works only for polygons, not polyline based on their docs – Matt Jun 05 '23 at 15:28