Google Play服务 - 位置Xamarin Android

时间:2017-05-24 17:10:27

标签: android xamarin google-play-services

我已在我的应用中实施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;
    }
}

每隔~2分钟就会调用一次GoogleManager类,但是来自BroadcastReciver的是AlarmReciever。

[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 个答案:

答案 0 :(得分:1)

如果您未要求高精度定义LocationRequest,则保险丝供应商将使用最低功率选项向您返回位置。这通常只是一个基于wifi的位置,只能像Google映射您的位置一样准确。

当然,手机需要启用wifi,手机和GPS /定位服务才能为融合提供商提供最佳效果。

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

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