c# - 将 FusedLocationApi 与 Xamarin 3 结合使用

标签 c# android xamarin.ios xamarin google-play-services

当我尝试从我的 Xamarin Activity 中使用 FusedLocationApi 时遇到了很多问题。此处列出的代码使用的方法 Location Xamarin已被标记为过时,因此无法编译。我的实现如下。我的问题是,如果这是这样做的方式,或者我是否忽略了一些更容易的事情? LocationHandler 由我的 Activity 使用,例如OnCreate、OnResume、OnPause 调用连接和断开连接方法。 OnChangedLocation 方法当然应该做一些更智能的事情。

using System;

using Android.Gms.Common;
using Android.Gms.Common.Apis;
using Android.Gms.Location;
using Android.Locations;
using Android.Util;
using Android.OS;
using Android.Content;


namespace WithKidsAndroid
{
    public class LocationHandler : Java.Lang.Object, IGoogleApiClientConnectionCallbacks, IGoogleApiClientOnConnectionFailedListener, Android.Gms.Location.ILocationListener
    {

        private IGoogleApiClient _googleAPI;
        private Context _context;

        public LocationHandler(Context context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            else
            {
                _context = context;
            }
            initializeGoogleAPI();
            LocRequest = new LocationRequest();
        }

        public LocationHandler(Context context, LocationRequest request)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            else
            {
                _context = context;
            }
            initializeGoogleAPI();
            LocRequest = request;
        }

        public LocationRequest LocRequest
        {
            get;
            set;
        }

        public void connectGoogleAPI()
        {
            System.Diagnostics.Debug.Assert(_googleAPI != null);

            if (!_googleAPI.IsConnectionCallbacksRegistered(this))
            {
                _googleAPI.RegisterConnectionCallbacks(this);
            }
            if (!_googleAPI.IsConnectionFailedListenerRegistered(this))
            {
                _googleAPI.RegisterConnectionFailedListener(this);
            }
            if (!_googleAPI.IsConnected || !_googleAPI.IsConnecting)
            {
                _googleAPI.Connect();
            }
        }

        public void disconnectGoogleAPI()
        {
            if (_googleAPI != null && _googleAPI.IsConnected)
            {
                if (_googleAPI.IsConnectionCallbacksRegistered(this))
                {
                    _googleAPI.UnregisterConnectionCallbacks(this);
                }
                if (_googleAPI.IsConnectionFailedListenerRegistered(this))
                {
                    _googleAPI.UnregisterConnectionFailedListener(this);
                }
                _googleAPI.Disconnect();
            }
        }


        public void OnConnected(Bundle connectionHint)
        {
            Log.Debug("LocationHandler", "logged connected", connectionHint);
            if (LocRequest == null)
            {
                throw new Exception("Unknown location request. Set this first by using property LocRequest or constructor.");
            }

            LocationServices.FusedLocationApi.RequestLocationUpdates(_googleAPI, LocRequest, this);
        }

        public void OnConnectionSuspended(int cause)
        {
            Log.Debug("LocationHandler", "logged OnConnectionSuspended", cause);

        }

        public void OnConnectionFailed(ConnectionResult result)
        {
            Log.Debug("LocationHandler", "logged OnConnectionFailed", result);

        }

        public void OnLocationChanged(Location location)
        {
            Log.Debug("LocationHandler", "logged location changed: " + location.ToString());
        }

        private void initializeGoogleAPI()
        {
            int queryResult = GooglePlayServicesUtil.IsGooglePlayServicesAvailable(_context);

            if (queryResult == ConnectionResult.Success)
            {
                _googleAPI = new GoogleApiClientBuilder(_context).AddApi(LocationServices.Api).AddConnectionCallbacks(this).AddOnConnectionFailedListener(this).Build();
            }
            else
            {
                var errorString = String.Format("There is a problem with Google Play Services on this device: {0} - {1}", queryResult, GooglePlayServicesUtil.GetErrorString(queryResult));
                Log.Error("WithKidsAndroid.LocationHandler", errorString);
                throw new Exception(errorString);
            }
        }


    }

}

最佳答案

我猜不是。我将关闭问题,但不会删除问题,因为人们随后可以找到有效的 LocationServices 示例。

关于c# - 将 FusedLocationApi 与 Xamarin 3 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872452/

相关文章:

c# - Parallel.ForEach MaxDegreeOfParallelism 增加 "Chunking"的奇怪行为

Android - 编辑 list 以禁用组件后,它将始终被禁用

ios - MonoTouch 将带有 alpha 的彩色 UIImage 转换为灰度和模糊?

c# - 从 XMLDocument 中的 xml 文件读取节点

c# - 如何使用 C# 隐藏 OpenXML 电子表格中的列?

java - 如何在android中添加 'java.awt.geom'类?

android - 访问 android wear 数据层时出错

ios - 为什么Scrollview和子view大小有时不一致

c# - iPhone - 未经授权的访问异常

c# - 使用 WEB API HttpClient 使用外部 API 然后以 Angular 显示反序列化结果是否可以接受?