【发布时间】:2015-10-28 12:43:42
【问题描述】:
我已将 OnLocationChanged(Location location) 实现为我的 Activity 中的覆盖方法,如图所示:
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
if (wifiSpotList != null) {
presenter.onLocationUpdated(wifiSpotList, mCurrentLocation);
centerInLoc(location);
}
}
问题是,即使设备没有移动,这个方法也会被触发。它总是以毫秒为间隔发生。
通过位置参数的调试器让我得到这个日志:
mTime = 1446035851646
mLongitude = -3.6923793
mLatitude = 40.5008861
mTime = 1446035856976
mLongitude = -3.6923596
mLatitude = 40.5008932
mTime = 1446035867327
mLongitude = -3.6923681
mLatitude = 40.5008987
mTime = 1446035877344
mLongitude = -3.6923778
mLatitude = 40.5008949
mTime = 1446035882442
mLongitude = -3.6923734
mLatitude = 40.5008926
...
编辑:
这是初始化 requestLocationUpdates 的代码:
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
this.locationUpdatesStarted = true;
}
编辑 mLLocationRequest:
protected void createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(LOCATION_REQUEST_INTERVAL);
mLocationRequest.setFastestInterval(LOCATION_REQUEST_FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
this.mLocationRequest = mLocationRequest;
}
该方法是因为经纬度变化而触发的吗? 为什么即使设备没有移动它也会触发? 我可以限制准确性吗? 有什么干净的方法来实现 onLocationChanged() 吗?
【问题讨论】:
-
告诉我你调用的代码
requestLocationUpdates()我想你已经设置了时间间隔。requestLocationUpdates()方法中的第二个参数。 -
我已将其添加到问题中。
-
您是否已将
setInterval()添加到您的mLocationRequest变量中? -
我也加了。
-
检查
LOCATION_REQUEST_INTERVAL将被分配给一个整数值,它是毫秒。对于LOCATION_REQUEST_INTERVAL的每个间隔时间段,它将更新位置,请参阅此link
标签: android coding-style google-maps-android-api-2