【问题标题】:How to use location listener?如何使用位置监听器?
【发布时间】:2017-02-13 02:45:19
【问题描述】:

我不清楚如何使用位置监听器。

我会这样做吗:

public Location actualLocation;

private class mLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        actualLocation = location;
    }

    public void onStatusChanged(String s, int i, Bundle b) {
    }

    public void onProviderDisabled(String s) {
    }

    public void onProviderEnabled(String s) {
    }
}

//Middle of code
currentLatitude = actualLocation.getLatitude()
currentLongitude = actualLocation.getLongitude()

或者这个:

public Location actualLocation;

private class mLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
    }

    public void onStatusChanged(String s, int i, Bundle b) {
    }

    public void onProviderDisabled(String s) {
    }

    public void onProviderEnabled(String s) {
    }
}

//Middle of code
actualLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

currentLatitude = actualLocation.getLatitude()
currentLongitude = actualLocation.getLongitude()

使用了一些未在此 sn-ps 代码中声明的变量。这是故意的。为了简单起见,我缩短了代码。

更新: 我的问题是:¿我是使用 getLastKnow 位置还是每次运行时手动保存位置以进行更新? ¿ 那也一样吗?

【问题讨论】:

  • 你想用LocationListener实现什么

标签: android location android-gps


【解决方案1】:

您需要调用 LocationManager.registerLocationUpdates。您将 LocationListener 传递给该调用,框架将在有新位置时调用这些函数。

【讨论】:

  • 我现在更新了我的问题,所以它更清楚了。我基本上想知道 locationUpdates 是否更新 getLastKnowLocation 还是每次更新时我都必须手动编写新位置?
  • 每次更新都写。如果您再次调用 get last known location 它会给您一个新的答案,但它不会更新旧结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多