我们如何检查折线是否存在于传单的地图边界内

How can we check if polyline exists within map bounds in leaflet

我正在传单地图上绘制来自 geojson 文件的线条。现在我需要根据当前视图读取存在于地图边界内的特性。

我看到了这个 link: https://github.com/stefanocudini/leaflet-list-markers/blob/master/src/leaflet-list-markers.js 它基本上使用 layer.getLatLng() 函数检查标记是否存在于边界内。尝试使用它,但在我的情况下它会抛出 "method does not exist"。

添加到地图后,是否有检查线要素是否存在于地图边界内的方法。

您可以检查是否 line's bounding box overlaps the map's bounding box:

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

请记住,这会检查边界框。如果您需要更准确的交点计算,您可能需要使用 TurfJS's intersect method.

你说你不能运行getLatLng()上一个L.Polyline,这并不奇怪。 Read the Leaflet documentation 仔细,你会发现标记有一个 getLatLng() 方法,折线有 getLatLngs()getBounds() 代替。