简体   繁体   English

如何从Leaflet中的标记获取地图框坐标?

[英]How to get map box coordinates from marker in Leaflet?

I have a static map and markers (without the ability to zoom and move). 我有一个静态地图和标记(无法缩放和移动)。 I need that each marker check the four points where the map ends. 我需要每个标记检查地图结束处的四个点。

Any idea? 任何想法?

What you need are called the map bounds. 您需要的就是地图边界。 The boundaries of the map. 地图的边界。 If you need those you can use the getBounds method of your L.Map instance which returns a L.LatLngBounds object: 如果需要这些,可以使用L.Map实例的getBounds方法,该方法返回L.LatLngBounds对象:

var bounds = map.getBounds();

You can use that object to get the coordinates of the corners of your map: 您可以使用该对象获取地图拐角的坐标:

var northWest = bounds.getNorthWest(),
    northEast = bounds.getNorthEast(),
    southWest = bounds.getSouthWest(),
    southEast = bounds.getSouthEast();

These methods return L.LatLng objects which contain the coordinates of those points: 这些方法返回包含这些点的坐标的L.LatLng对象:

northWest.lat // contains latitude of northwestern point
northWest.lng // contains longitude of northwestern point

I'm unsure as to why you would want to let each marker check the bounds since you stated that the map is static, which means every marker would return the same results. 我不确定您为什么要让每个标记检查边界,因为您说地图是静态的,这意味着每个标记都将返回相同的结果。 You could check the bounds once and use the results with all the markers. 您可以检查一次边界,并将结果与​​所有标记一起使用。

Reference: 参考:

L.Map.getBounds(): http://leafletjs.com/reference.html#map-getbounds L.Map.getBounds(): http ://leafletjs.com/reference.html#map-getbounds

L.LatLngBounds: http://leafletjs.com/reference.html#latlngbounds L.LatLngBounds: http ://leafletjs.com/reference.html#latlngbounds

L.LatLng: http://leafletjs.com/reference.html#latlng L.LatLng: http ://leafletjs.com/reference.html#latlng

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM