我们已经开发了ola和uber等交付应用程序。骑手可以下订单并交付给客户的地方。我们已经初始化位置变量,从而创建了一个android服务(Background Service)。我们通过3种方法(网络提供商,Gps提供商,Google融合的位置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提供商和融合位置提供商。我正在获取位置,但准确性较低。我还维护了最近10个连续位置的列表。当我收到第11个位置时,我搜索了列表,如果发现所有位置都相同(这意味着位置卡住了),那么我清除了列表,并通过停止并使用新连接重新启动服务来断开位置连接。
答案 0 :(得分:2)
Android有restrictions用于从后台服务获取位置更新。您要么必须从前台活动中获取位置,要么将服务更改为具有固定通知的前台服务,以获取定期的位置更新。
位置准确性问题也可能是由于用户未在设备中设置高精度位置模式。这将检查用户设备中是否启用了位置。如果启用了位置但未启用高精度,将显示对话框。
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;
}
}
});
}