area estimation in viewpoint of map using leaflet

ぃ、小莉子 提交于 2019-12-13 05:15:24

问题


I have a map with several areas denoted by a polygon. I want to enable some options and display some markers when the polygon is right in the middle of map and the right zoom level. How can I detect this. I can get the map bounds but have no idea how to use it to check if the polygon is in the map.

Thanks for the help!


回答1:


First, you'll need to get the bounds of your polygon. Use the getBounds function described here to get the bounds of your polygon.

// This will return an L.LatLngBounds object
var polygonBounds = polygon.getBounds();

Then, check to see if the bounds of your polygon are contained within the bounds of your map.

// Getting the bounds of the map (you know how to do this)
var mapBounds = map.getBounds();
// Now, determine if the polygon bounds are within the map bounds
var contains = mapBounds.contains(polygonBounds);

This contains boolean will now be true if the map bounds completely contains your polygon, and false if it does not.



来源:https://stackoverflow.com/questions/18628647/area-estimation-in-viewpoint-of-map-using-leaflet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!