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(在回答的最末端)。