9

见这个例子:检查纬度和经度是一个圆内

enter image description here

我想知道的是:

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

我宁愿如果你能给我在Java或专为Android与谷歌地图API V2的代码示例

+0

嘛,至于第二个问题去,圆是从一个点,该中心的距离相等的点的集合刚。如果它在圆圈内,则意味着它离中心更近(距离更小),如果它在外侧,则距离大于半径。在你的例子中,半径是10公里。 –

回答

21

基本上你需要什么,是在地图上点之间的距离:

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

+0

嗨,我想实现与java相同的功能我想知道的是: 1)如何创建一个区域(圆圈)给定经纬度和距离(以米为单位) 2)如何检查(计算),如果纬度和经度的内部或区域 –

+0

外@MangeshMandavgane你只需要复制两个功能称为“computeDistanceAndBearing”和“distanceBetween” https://android.googlesource.com/platform/frameworks/base/ +/refs/heads/master/location/java/android/location/Location.java – Flo354

+0

@ Flo354,谢谢回复我检查了您的建议网址代码。这是非常有帮助的代码,但我已经完成了4个月前的实现,我将这个http://stackoverflow.com/questions/120283/how-can-i-measure-distance-and-create-a-bounding-box- based-two-latitudelongi –

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

???根据定义,圆上的每个点距离中心10公里,区域之间的距离兴趣和当前位置是圆心和当前位置之间的界限 –

0

如果你的意思是“如何创建区域”,要在地图上绘制的区域,你会发现就在地图V2参考doc for the class Circle一个例子。

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

还用于在壳体的区域是多边形而不是圆的代码实例参见here(在回答的最末端)。