Determine if location is within a certain region i

2019-08-06 22:20发布

问题:

In the Google Map shown above, there are 2 markers pointed by green arrows. They are the centers of 2 different regions indicated by blue circles in dotted lines. The marker pointed by an orange arrow lies within one of the aforementioned areas.

This brings up a problem.

Suppose there is a long list of locations of region center in the database, and all regions have the same fixed radius. How can I implement the computation such that the system can tell me whether an arbitrary location on Google Map lies within at least one of the regions? Iterating through the list of regions with a for-loop or while-loop in the source code is not an option, as performance is the first priority in my application.

Are there any existing Google Map APIs or libraries which provide such functionalities?

I am going to use Ionic for client and .NET C# for server, if it matters.

回答1:

You can use Google Maps Geometry Library.

If the area is a circle you can calculate the distance from the center, see for example on SO: How to detect if a point is in a Circle?

If the area is a Polygon see this example: https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation