Java 检查纬度和经度是否在圆圈内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22063842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a latitude and longitude is within a circle
提问by Peter Warbo
See this illustration:
请参阅此插图:
What I would like to know is:
我想知道的是:
- How to create an area (circle) when given a latitude and longitude and the distance (10 kilometers)
- How to check (calculate) if a latitude and longitude is either inside or outside the area
- 如何在给定纬度和经度以及距离(10 公里)时创建区域(圆)
- 如何检查(计算)纬度和经度是在区域内还是区域外
I would prefer if you can give me code example in Java or specifically for Android with Google Maps API V2
我更希望您能提供 Java 代码示例或专门针对带有 Google Maps API V2 的 Android 代码示例
采纳答案by flx
What you basically need, is the distance between two points on the map:
您基本上需要的是地图上两点之间的距离:
float[] results = new float[1];
Location.distanceBetween(centerLatitude, centerLongitude, testLatitude, testLongitude, results);
float distanceInMeters = results[0];
boolean isWithin10km = distanceInMeters < 10000;
If you have already Location
objects:
如果您已经有Location
对象:
Location center;
Location test;
float distanceInMeters = center.distanceTo(test);
boolean isWithin10km = distanceInMeters < 10000;
Here is the interesting part of the API used: https://developer.android.com/reference/android/location/Location.html
这是所用 API 的有趣部分:https: //developer.android.com/reference/android/location/Location.html
回答by Seshu Vinay
Have you gone through the new GeoFencing API. It should help you. Normal implementation takes a lot of time. Thisshould help you implementing it easily.
您是否了解过新的GeoFencing API。它应该可以帮助你。正常的实施需要很多时间。这应该可以帮助您轻松实现它。
回答by Android Newbie
see https://developer.android.com/reference/android/location/Location.html
见https://developer.android.com/reference/android/location/Location.html
Location areaOfIinterest = new Location;
Location currentPosition = new Location;
areaOfIinterest.setLatitude(aoiLat);
areaOfIinterest.setLongitude(aoiLong);
currentPosition.setLatitude(myLat);
currentPosition.setLongitude(myLong);
float dist = areaOfIinterest.distanceTo(currentPosition);
return (dist < 10000);
回答by user2808624
If you mean by "How to create an area", that you want to draw the area on the map, you will find an example right in the map V2 reference doc for the class Circle.
如果您的意思是“如何创建区域”,即您想在地图上绘制该区域,您将在地图 V2 参考文档中找到 Circle 类的示例。
For checking whether the distance between the center of the circle and your point is greater than 10 km I would suggest to use the static method Location.distanceBetween(...)as it avoids unnecessary object creations.
为了检查圆心和您的点之间的距离是否大于 10 公里,我建议使用静态方法Location.distanceBetween(...)因为它避免了不必要的对象创建。
See also here(at the very end of the answer) for a code example in case the area is a polygon rather than a circle.
另请参见此处(在答案的最后)以获取代码示例,以防该区域是多边形而不是圆形。
回答by gulab ahirwar
Check this:
检查这个:
private boolean isMarkerOutsideCircle(LatLng centerLatLng, LatLng draggedLatLng, double radius) {
float[] distances = new float[1];
Location.distanceBetween(centerLatLng.latitude,
centerLatLng.longitude,
draggedLatLng.latitude,
draggedLatLng.longitude, distances);
return radius < distances[0];
}