getLastLocation()从LocationClient始终返回null [英] getLastLocation() from LocationClient always returns null

查看:1300
本文介绍了getLastLocation()从LocationClient始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 LocationClient 来得到网络用户位置(无GPS)。
我实例化 LocationClient 创建我的片段中

  mLocationClient =新LocationClient(getActivity(),这,这);
 mLocationClient.connect();

再后来

 如果(mLocationClient.isConnected())
  {
    currentLocation = mLocationClient.getLastLocation();
  }

我相信,它连接,但 getLastLocation()收益总是空。

然而,它的工作原理使用的LocationManager

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

我知道我可以只使用位置更新的监听器,但我想知道为什么 getLastLocation()始终返回null。


解决方案

文件说: -

 公共场所getLastLocation()


  

返回的最好最新的位置目前已经上市。如果一个
  位置不可用,这应该发生很少,空意愿
  返回。可用的最佳精度,同时尊重的位置
  权限将被退回。此方法提供了一种简化的方式来
  得到的位置。它特别适合用于执行操作的应用程序
  不要求精确的定位和不希望保持
  额外的逻辑位置更新。


您必须使用 onLocationChange()方法得到的位置: -

  @覆盖
公共无效onLocationChanged(决赛地点){
      mCurrentLocation =位置;
}

I'm using the LocationClient to get the user location from the network (no gps). I instantiate the LocationClient during the creation of my fragment

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

And then later

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

I am sure that it is connected, but the getLastLocation() returns null always.

However, it works using the LocationManager.

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

I know I could just use a listener for location updates, but I would like to understand why getLastLocation() always returns null.

解决方案

Document says :-

public Location getLastLocation ()

Returns the best most recent location currently available. If a location is not available, which should happen very rarely, null will be returned. The best accuracy available while respecting the location permissions will be returned. This method provides a simplified way to get location. It is particularly well suited for applications that do not require an accurate location and that do not want to maintain extra logic for location updates.

You have to use onLocationChange() method to get location:-

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

这篇关于getLastLocation()从LocationClient始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆