2017-05-24 97 views
2

我已經在我的應用程序中實施Google Play服務後臺位置跟蹤。我之前使用過位置管理器,但在某些設備上無法使用。Google Play服務 - 位置Xamarin Android

使用設備時有很奇怪的事情。由於我已經發布了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類獲取調用每次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); 
     } 
} 

背景定位跟蹤工作正常,但有時有位置日誌之間大的偏差。然而,使用位置管理器時,它工作得很好。

有沒有人遇到過類似的問題?

編輯: 我跑去了,我發現每次每個地點都去了同一個地方。這裏是屏幕: enter image description here

回答

1

定義當你的LocationRequest如果你不要求精度高,則保險提供商將採用最低功耗選項的位置還給你。這通常是基於wifi的唯一位置,只有Google對您的位置進行了映射。

當然,手機需要開啓wifi,蜂窩和GPS /位置服務,以便與融合供應商一起提供最佳效果。

AccessFineLocation添加到您的清單權限中,並將您的LocationRequest優先級設置爲高精度,以啓動GPS並對您的位置進行採樣並重新檢查結果。

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

感謝您的回覆!不幸的是我已經設置了PriorityHightAccuracy;((見我的編輯) – miechooy