area estimation in viewpoint of map using leaflet

2019-08-17 06:10发布

问题:

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.