2012-11-20 31 views
0

可能重複:
What is the simplest and most robust way to get the user’s current location in Android?使用位置API中的Android

我有研究,針對Android的定位API,並試圖具有使用locationChanged方法,並提供了一個基本的應用程序跟蹤使用位置的最簡單的解決方案。

如何可以跟蹤用戶的以下行爲在同樣的情況下

  • 用戶進入一個新的位置
  • 用戶離開當前位置

也對以下幾個因素:

  • 新位置不在內說x米老位置
  • 和比較過去和新的位置

回答

0

這是不壞所需的屬性。關於這個問題的官方Android SDK其實很不錯。 Official Google Documentation on it

基本上,它使用的接口運行功能,因爲每種情況都會出現。因此,只要位置根據任何參數觸發關閉而改變,就會調用onLocationChanged。此外,我會建議只是玩不同的提供商類型

最難的部分是距離的一部分。有方程式用來計算經度和緯度的距​​離。

從文檔:

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) {} 

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
    }; 

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
相關問題