【问题标题】:getLastLocation() from LocationClient always returns null [duplicate]来自 LocationClient 的 getLastLocation() 始终返回 null [重复]
【发布时间】:2014-06-21 09:58:15
【问题描述】:

我正在使用LocationClient 从网络获取用户位置(无 GPS)。 我在创建片段时实例化了LocationClient

 mLocationClient = new LocationClient(getActivity(), this, this);
 mLocationClient.connect();

后来

  if (mLocationClient.isConnected())
  {
    currentLocation = mLocationClient.getLastLocation();
  }

我确定它已连接,但 getLastLocation() 总是返回 null。

但是,它可以使用 LocationManager

    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

我知道我可以只使用监听器来更新位置,但我想了解为什么 getLastLocation() 总是返回 null。

【问题讨论】:

  • 你是在真机还是模拟器中尝试?
  • @Kedarnath 真机,Galaxy S3
  • @HemantChandDungriyal 我看到了,但很奇怪,因为我在 LocationClient 或多或少初始化 5 秒后调用该函数
  • 打开谷歌地图初始化并访问某个位置,然后尝试你的应用程序。
  • 我做到了,但没有任何改变:(

标签: android geolocation


【解决方案1】:

文件说:-

public Location getLastLocation ()

返回当前可用的最近的最佳位置。如果一个 位置不可用,这应该很少发生,null 将 被退回。在尊重位置的同时获得最佳精度 权限将被返回。该方法提供了一种简化的方法 获取位置。它特别适用于执行以下操作的应用程序 不需要准确的位置并且不想维护 位置更新的额外逻辑。

你必须使用onLocationChange()方法来获取位置:-

@Override
public void onLocationChanged(final Location location) {
      mCurrentLocation = location;
}

【讨论】:

    猜你喜欢
    • 2013-07-27
    • 2015-08-15
    • 1970-01-01
    • 2021-03-05
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多