android - Google Play 服务 - 定位 Xamarin Android

标签 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;
    }
}

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/

相关文章:

android - 使用 Google Play 服务构建 Xamarin.Android 项目时出错

c# - 从非子布局动态地将 EditText 添加到另一个 LinearLayout

android - 将 PlayServices/Firebase 从 10.0.1 更新到 10.2.0 时出现问题

android - 检索 openstreetmap 中的 maxspeed 标签

android - android studio 中不是全宽工具栏?

android - 如何使用 Mobile Vision API 获取图像中文本的位置?

java - android gms 大方法计数。如何避免呢?

android - NearbyBackgroundBeacons 演示在订阅时返回未知状态代码 2801

android - Android 与其他设备之间的蓝牙 SPP、UUID 和 PIN 问题

android - 是否有适合 Android 开发的首选 Linux 发行版?