Android 当位置未随LocationClient更改时,如何调用onLocationChanged?

Android 当位置未随LocationClient更改时,如何调用onLocationChanged?,android,android-location,Android,Android Location,我已经成功地实现了android位置示例 我可以通过单击按钮请求位置更新,onLocationChanged方法将被触发以使用当前位置更新地图视图 但是考虑下面的问题。当手机位置不变时,将不再触发onLocationChange。当用户触摸地图视图并手动滑动到另一个位置时,另一个位置请求将不会触发onLocationChanged,并且地图视图将不会获取当前位置,因为手机位置没有更改 我的问题是,如何在onLocationChanged中接收位置差为0的位置?我使用的是LocationClien

我已经成功地实现了android位置示例

我可以通过单击按钮请求位置更新,onLocationChanged方法将被触发以使用当前位置更新地图视图

但是考虑下面的问题。当手机位置不变时,将不再触发onLocationChange。当用户触摸地图视图并手动滑动到另一个位置时,另一个位置请求将不会触发onLocationChanged,并且地图视图将不会获取当前位置,因为手机位置没有更改

我的问题是,如何在onLocationChanged中接收位置差为0的位置?我使用的是LocationClient和LocationRequest类,而不是LocationManager,因此这不起作用:

manager.requestLocationUpdates(best, 10000, 1, locationListener);

有什么想法吗?

使用
getLastLocation()
locationClient的方法,它将为您提供最后一个已知的位置,但有时可能为空,我观察到在某些设备中,当我重新启动设备并在启动gps或location client方法之前调用该函数时,它返回null。

在这种情况下,您可以调用
getLastKnownLocation
,但您必须检测该状态,只需尝试包装提供位置的类,并在该类更新为R时将其封装
requestLocationUpdates
监听更新,一旦未触发更新,就会调用
getLastKnownLocation

因此,您可以简单地用包含
lastLocation
或best
location
字段的类来包装它

public class MyLocationProvider implements LocalisationListener{
   private Location bestLocation = null;
   private LocationManager locationManager;
   //constructor and all that stuff...

   public Location getBestLocation(){
     if(bestLocation == null)
       return locationManager.getLastKnownLocation();
     else 
       return bestLocation;

}

//locationListener methods... that should save their result in `bestLocation` field.

}
这是我的想法的简单草稿。。。。我没有编译它;)


在这种情况下,您可以调用
getLastKnownLocation
,但您必须检测到该状态,只需尝试包装您提供位置的类,在更新时使用RequestLocationUpdates侦听更新,在未触发时调用getLastKnownLocation@Rafik991我知道,但我不知道是否会触发onLocationChanged。只请求lastLocation和请求更新安全吗?请看下面我的草稿;)这不起作用,因为当触发onLocationChanged时,bestLocation不为null,因此您可以使用
lastLocation
对其进行初始化,但如何区分onLocationChanged未被调用的情况(因为位置未被调用,因为您没有收到gps信号,gps无法找到gps信号)修理