我们开发了类似 ola 和 uber 的送货应用。骑手可以在哪里下订单并交付给客户。我们已经创建了一个 android 服务(后台服务),因为我们已经初始化了我们的位置变量。我们通过 3 种方法获取位置,Network Provider,Gps Provider,Google Fused location api。这三个我们都用来获取一个位置,当我们获取一个位置时,它存储到我们的成员变量(稍后它在副项目中使用)并且相同的位置被保存到我们针对该骑手的服务器。我们具体面临 2 或 3 个问题。 1. 有时用户位置是正确的,在 20-30 秒内我们得到了一些错误的位置,准确度比方说 > 50 或 100。 2.某些时间位置卡住几分钟或几个小时。
我们正在使用 Google 指定的特定位置策略。下面是示例。
private static final int TWO_MINUTES = 1000 * 60 * 2;
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
if (isMoreAccurate)
{
return true;
}
else if (isNewer && !isLessAccurate)
{
return true;
}
else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)
{
return true;
}
return false;
我不知道下一步该做什么。
我已经集成了 Network Provide 、 GPS provider 和 fused location provider 。我正在获取位置,但不太准确。我还保留了最近 10 个连续位置的列表。当我收到第 11 个位置时,我搜索了列表,如果发现所有位置都相同(这意味着位置被卡住了),那么我清除了我的列表并通过停止并使用新连接再次重新启动服务来删除位置连接。
最佳答案
Android 有 restrictions用于从后台服务获取位置更新。您要么必须从前台 Activity 获取位置,要么将您的服务更改为带有固定通知的前台服务,以获取定期位置更新。
定位精度问题也可能是由于用户未在设备中设置高精度定位模式。这将检查用户设备中是否启用了位置。如果启用位置但未启用高精度,将显示对话框。
private static final int REQUEST_CHECK_SETTINGS = 001;
private void displayLocationSettingsRequest(Context context) {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
//Location settings satisfied
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
//Location settings are not satisfied. Show the user a dialog to upgrade location settings
try {
status.startResolutionForResult(Activity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
LogUtil.i("PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//Location settings are inadequate, and cannot be fixed here. Dialog not created.
Toast.makeText(Activity.this, "Error enabling location. Please try again", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
关于android - 无法在 Android 设备上获取准确位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56256855/