检查纬度和经度是否在圆圈内

问题描述 投票:17回答:5

看这个插图:

我想知道的是:

  1. 给定纬度和经度以及距离(10公里)时如何创建区域(圆圈)
  2. 如何检查(计算)纬度和经度是在区域内还是在区域外

如果您能使用Google Maps API V2为我提供Java代码示例或专门针对Android的代码示例,我更愿意

java android google-maps-android-api-2 latitude-longitude area
5个回答
27
投票

你基本上需要的是地图上两点之间的距离:

float[] results = new float[1];
Location.distanceBetween(centerLatitude, centerLongitude, testLatitude, testLongitude, results);
float distanceInMeters = results[0];
boolean isWithin10km = distanceInMeters < 10000;

如果你已经有Location对象:

Location center;
Location test;
float distanceInMeters = center.distanceTo(test);
boolean isWithin10km = distanceInMeters < 10000;

以下是使用的API的有趣部分:https://developer.android.com/reference/android/location/Location.html


1
投票

你有没有通过新的GeoFencing API。它应该对你有所帮助。正常实施需要花费很多时间。 This应该帮助您轻松实现它。


1
投票

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);

0
投票

如果您的意思是“如何创建一个区域”,那么您想在地图上绘制区域,您将在地图V2参考doc for the class Circle中找到一个示例。

为了检查圆心和点之间的距离是否大于10 km,我建议使用静态方法Location.distanceBetween(...),因为它避免了不必要的对象创建。

有关代码示例,请参阅here(在答案的最后),以防该区域是多边形而不是圆形。


0
投票

检查一下:

 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];
}
© www.soinside.com 2019 - 2024. All rights reserved.