我已經在我的應用程序中實施Google Play服務後臺位置跟蹤。我之前使用過位置管理器,但在某些設備上無法使用。Google Play服務 - 位置Xamarin Android
使用設備時有很奇怪的事情。由於我已經發布了GooglePlayServices的大部分設備,包括礦井發送位置日誌的偏差很大。
有數據庫
//...
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類獲取調用每次1〜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);
}
}
背景定位跟蹤工作正常,但有時有位置日誌之間大的偏差。然而,使用位置管理器時,它工作得很好。
有沒有人遇到過類似的問題?
編輯: 我跑去了,我發現每次每個地點都去了同一個地方。這裏是屏幕:
感謝您的回覆!不幸的是我已經設置了PriorityHightAccuracy;((見我的編輯) – miechooy