看这个插图:
我想知道的是:
如果您能使用Google Maps API V2为我提供Java代码示例或专门针对Android的代码示例,我更愿意
你基本上需要的是地图上两点之间的距离:
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
你有没有通过新的GeoFencing API。它应该对你有所帮助。正常实施需要花费很多时间。 This应该帮助您轻松实现它。
见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);
如果您的意思是“如何创建一个区域”,那么您想在地图上绘制区域,您将在地图V2参考doc for the class Circle中找到一个示例。
为了检查圆心和点之间的距离是否大于10 km,我建议使用静态方法Location.distanceBetween(...),因为它避免了不必要的对象创建。
有关代码示例,请参阅here(在答案的最后),以防该区域是多边形而不是圆形。
检查一下:
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];
}