6

我正在开发一个 Android 应用程序,其中当前和精确的位置是关键。检测到 GPS 定位后,它会移动到下一个活动。

问题是我在户外对其进行了测试,发现有时它只使用了以前修复的位置。

所以我在问什么;除了 getLastKnownLocation 之外,还有其他逻辑可以从 LocationManager 获取位置吗?我还没有找到一个看起来不像的例子

    Location location = locationManager.getLastKnownLocation(provider);

谢谢!

4

3 回答 3

2

您可以使用LocationListener然后调用locationManager.requestLocationUpdates()传递您自己的侦听器实现。

于 2013-07-09T13:08:31.120 回答
2

这是官方指南中的示例代码(稍作修改):

// 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) {
         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.GPS_PROVIDER, 0, 0, locationListener);

在这里,您只需从 GPS 请求更新,一旦收到位置,您就会收到回拨电话。

于 2013-07-09T13:09:11.067 回答
1

Google has released new Location API, check this link out https://developer.android.com/training/location/index.html,

for you question, you need to implement location update callback to get current location. https://developer.android.com/training/location/receive-location-updates.html

于 2013-07-09T13:11:06.367 回答