2017-05-24 96 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