gpt4 book ai didi

android - Google Play 服务 - 定位 Xamarin Android

转载 作者:行者123 更新时间:2023-11-29 01:09:38 24 4
gpt4 key购买 nike

我在我的应用程序中实现了 Google Play 服务后台位置跟踪。我以前使用过位置管理器,但它在某些设备上不起作用。

使用设备时出现了非常奇怪的事情。自从我发布了 GooglePlayServices 以来,包括我的设备在内的大多数设备发送的位置日志都有很大的偏差。

有我今天的路图: enter image description here

数据库中有位置

//...
Latitude Longitude Provider Accuracy
51,0994253 17,1077168 fused 21,5179996490479
51,0994253 17,1077168 fused 21,5179996490479
51,0996427 17,1076683 fused 21,7150001525879
51,0996427 17,1076683 fused 21,7150001525879
51,0996427 17,1076683 fused 21,7150001525879
51,1003416 17,1079516 fused 8
51,1003416 17,1079516 fused 8
51,1003416 17,1079516 fused 8
51,1008037 17,1083013 fused 8
51,1008037 17,1083013 fused 8
51,1008037 17,1083013 fused 8
51,0997649 17,0375168 fused 20 //Strange point
51,0997649 17,0375168 fused 20
51,0997649 17,0375168 fused 20
51,1094489 17,065886 fused 21,1340007781982
51,1094489 17,065886 fused 21,1340007781982
51,1094489 17,065886 fused 21,1340007781982
//....

如您所见,奇怪的位置的准确度等于 20,它看起来非常非常奇怪。

实现:

public class GoogleApiLocationManager : Java.Lang.Object, IResultCallback, ILocationListener
{
public GoogleApiLocationManager(Context context, GoogleApiManagerMode requestMode)
{
_requestMode = requestMode;
_context = context;
_client = new GoogleApiClient.Builder(context)
.AddConnectionCallbacks(OnGoogleConnected)
.AddOnConnectionFailedListener(OnGoogleConnectFailed)
.AddApi(LocationServices.API)
.Build();
_client.Connect();
}

public void OnGoogleConnected()
{
switch (_requestMode)
{
case GoogleApiManagerMode.LastKnownLocation:
SaveLastKnownLocation();
break;
case GoogleApiManagerMode.RequestNewLocation:
RunLocationRequest();
break;
}
}

public void OnResult(Java.Lang.Object result)
{
var locationSettingsResult = (LocationSettingsResult)result;
try
{
switch (locationSettingsResult.Status.StatusCode)
{
case CommonStatusCodes.Success:
LocationServices.FusedLocationApi.RequestLocationUpdates(_client, _locationRequest, this);
break;
case CommonStatusCodes.ResolutionRequired:
Toast.MakeText(_context, "Could not get location. Please enable high accuracy in location settings", ToastLength.Short);
var intent = new Intent(Settings.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
_context.StartActivity(intent);
break;
case LocationSettingsStatusCodes.SettingsChangeUnavailable:
//TODO:
break;
}
}
catch (Exception e)
{
//TODO:
}
}

private void SaveLastKnownLocation()
{
//Store to database
_client.Disconnect();
}

private void RunLocationRequest()
{
_locationRequest = CreateLocationRequest();
var builder = new
LocationSettingsRequest.Builder().AddLocationRequest(_locationRequest);
var pendingResult = LocationServices.SettingsApi.CheckLocationSettings(_client, builder.Build());
pendingResult.SetResultCallback(this);
}

private LocationRequest CreateLocationRequest()
{
_locationRequest = new LocationRequest();
_locationRequest.SetInterval((long)_triggerInterval.TotalMilliseconds);
_locationRequest.SetFastestInterval((long)_triggerInterval.TotalMilliseconds / 2);
_locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);

return _locationRequest;
}
}

GoogleManager 类每约 2 分钟被调用一次,但 AlarmReciever 是 BroadcastReciver 固有的。

[BroadcastReceiver]
public class AlarmReciver : BroadcastReceiver
{
//some login to wakeup each 2 minutes...

//runs google api manager each period
private void RunLocationManager()
{
new GoogleApiLocationManager(_context, GoogleApiManagerMode.LastKnownLocation);
new GoogleApiLocationManager(_context, GoogleApiManagerMode.RequestNewLocation);
}
}

后台位置跟踪工作正常,但位置日志之间有时会有很大的偏差。但是,在使用被贬低的位置管理器时,它工作得非常好。

有没有人遇到过类似的问题?

编辑:我去运行,发现每次位置都去同一个地方。这是屏幕: enter image description here

最佳答案

在定义您的 LocationRequest 时,如果您不要求高精度,则 fuse 提供商将使用最低功率选项向您返回位置。这通常是一个仅基于 wifi 的位置,其准确度取决于 Google 绘制的您的位置。

当然,手机需要打开 wifi、蜂窝和 GPS/定位服务,才能通过融合提供商提供最佳结果。

AccessFineLocation 添加到您的 list 权限中,并将您的 LocationRequest 优先级设置为高精度,以便为 GPS 供电并对您的位置进行采样并重新检查您的结果。

LocationRequest _locationRequest = new LocationRequest()
.SetPriority(LocationRequest.PriorityHighAccuracy);

关于android - Google Play 服务 - 定位 Xamarin Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44164748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com