2013-05-31 136 views

回答

0

你需要有三樣東西:

  1. 一個Location物體的位置這個圈子的中心。
  2. Location對象與當前位置。
  3. 圓的半徑。

然後,您可以使用方法Location.distanceTo(Location)並將結果與​​半徑進行比較。

例子:

所以我假設你已經在你的地圖爲Circle參考:

Circle circle; 

然後,我想你的活動實現LocationListener和使用方法onLocationChanged(Location)得到每次的通知位置變化。在這種方法中,你可以做這樣的事情:(未經測試的代碼)

@Override 
public void onLocationChanged(Location location) { 
    Location l = new Location(""); 
    l.setLatitude(circle.getCenter().latitude); 
    l.setLongitude(circle.getCenter().longitude); 
    if (location.distanceTo(l) > circle.getRadius()) { 
     // outside of circle 
    } else { 
     // inside 
    } 
} 

這樣的事情。

+0

是啊,請分享一個例子 –

+0

檢查編輯,請。 – zbr

+0

請檢查答案,如果它對你有幫助。 – zbr

1

如果您想使用地理圍欄功能,如果這是您的應用程序的目標,請使用此選項。

Geofencing in android

相關問題