【问题标题】:Get user current and exact location using GPS [duplicate]使用 GPS 获取用户当前和准确的位置 [重复]
【发布时间】:2015-12-07 09:29:40
【问题描述】:

对于我的新应用,我需要使用 GPS 获取用户位置。为此,我使用fusedLocationProviderApi.getLastLocation。大多数时候我收到null

但后来我意识到,所有 API 都是为了在位置更改时收到通知。但我想在应用程序中获取用户的当前确切位置(考虑用户不移动)。

如何获取 Location 对象,这样我就不会每次都得到null

【问题讨论】:

  • 是的,我同意关于获取位置的简短想法。但我的观点是,对于上述实现,每次位置更改都会调用 onLocationChanged,而不仅仅是获取常设用户的位置。还是我完全错了?
  • 能否请您更新您的 locationProvider 也有问题。
  • onLocationChanged() 将在位置解析后触发。用户无需实际移动。
  • Mike M. 感谢您的洞察力 :)

标签: android gps


【解决方案1】:

您可以在OnCreate()方法中使用此代码获取位置

// 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);

【讨论】:

  • 感谢您的回答。我会尝试解决方案:)
  • 如果您发现它有助于完整投票或接受答案
  • 接受您的解决方案。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多