无法在android设备上获取准确位置
我们已经开发了一个像ola和uber这样的交付应用程序。在这里,rider可以接受订单并交付给客户。我们已经创建了一个android服务(后台服务),我们已经初始化了位置变量。我们通过3种方法获得位置,网络提供商,Gps提供商,谷歌融合定位api。这三种方法都用来获取一个位置,当我们获取一个位置时,它会存储到我们的成员变量中(后来在side项目中使用),并且相同的位置会保存到我们的服务器中。我们具体面临2到3个问题。 1.一段时间内,用户位置是正确的,在20-30秒内,我们得到了一些错误的位置,精度超过50或100。 2.某些时间地点被阻塞了几分钟或延长了一小时 我们正在使用谷歌规定的特定定位策略。下面是示例无法在android设备上获取准确位置,android,gps,location,Android,Gps,Location,我们已经开发了一个像ola和uber这样的交付应用程序。在这里,rider可以接受订单并交付给客户。我们已经创建了一个android服务(后台服务),我们已经初始化了位置变量。我们通过3种方法获得位置,网络提供商,Gps提供商,谷歌融合定位api。这三种方法都用来获取一个位置,当我们获取一个位置时,它会存储到我们的成员变量中(后来在side项目中使用),并且相同的位置会保存到我们的服务器中。我们具体面临2到3个问题。 1.一段时间内,用户位置是正确的,在20-30秒内,我们得到了一些错误的位置,
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;
private static final int两分钟=1000*60*2;
/**确定一个位置读取是否优于当前位置修复
*@param location要计算的新位置
*@param currentBestLocation当前位置修复程序,您要将新位置修复程序与之进行比较
*/
受保护的布尔值isBetterLocation(位置位置,位置currentBestLocation){
如果(currentBestLocation==null){
//新位置总比没有位置好
返回true;
}
//检查新的位置修复程序是较新的还是较旧的
long-timeDelta=location.getTime()-currentBestLocation.getTime();
布尔值isSignificantlyNewer=时间增量>两分钟;
布尔值Issignificantlolder=时间增量<-2_分钟;
布尔值isNewer=timeDelta>0;
//如果距离当前位置已超过两分钟,请使用新位置
//因为用户可能已经移动了
如果(非常重要){
返回true;
//如果新位置比原来的位置早两分钟以上,情况肯定会更糟
}else if(Issignificantlolder){
返回false;
}
//检查新的位置修复是否更精确
int accuracyDelta=(int)(location.getAccuracy()-currentBestLocation.getAccuracy());
布尔值IslesAccurate=accuracyDelta>0;
布尔值IsmorePrecision=精度偏差<0;
布尔值不太精确=精度偏差>200;
//检查新旧位置是否来自同一提供商
布尔值isFromSameProvider=isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
//结合及时性和准确性确定定位质量
如果(更准确){
返回true;
}else if(isNewer&!islesAccurate){
返回true;
}else if(较新且不太准确且来自SameProvider){
返回true;
}
返回false;
}
/**检查两个提供程序是否相同*/
专用布尔值isSameProvider(字符串提供程序1、字符串提供程序2){
if(provider1==null){
返回provider2==null;
}
返回provider1.equals(provider2);
}
如果(更准确)
{
返回true;
}
else if(isNewer&!islesAccurate)
{
返回true;
}
else if(较新且不太准确且来自SameProvider)
{
返回true;
}
返回false;
我不知道下一步该做什么
我有集成网络提供商、GPS提供商和融合定位提供商。我得到了位置,但不太准确。我还保存了最近10个连续地点的清单。当我收到第11个位置时,我搜索了列表,如果发现所有位置都相同(这意味着位置被阻塞),那么我清除了列表,并通过停止并重新启动新连接来删除位置连接 Android拥有从后台服务获取位置更新的功能。您必须从前台活动获取位置,或者将您的服务更改为具有固定通知的前台服务,以获得定期位置更新
定位精度问题也可能是由于用户未在设备中设置高精度定位模式。这将检查用户设备中是否启用了位置。如果启用了位置但未启用高精度,则将显示对话框
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;
}
}
});
}
private static final int REQUEST\u CHECK\u SETTINGS=001;
私有void displayLocationSettingsRequest(上下文){
GoogleAppClient GoogleAppClient=新的GoogleAppClient.Builder(上下文)
.addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest LocationRequest=LocationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
locationRequest.setInterval(10000);
locationRequest.SetFastTestInterval(10000/2);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult=LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
最终状态状态=result.getStatus();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足位置设置
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。向用户显示一个对话框以升级位置设置
试一试{
status.startResolutionForResult(Activity.this,REQUEST\u CHECK\u设置);
}catch(IntentSender.sendtintentexe){
LogUtil.i(“PendingEvent无法执行请求”);
}