Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FusedLocationApi with Xamarin 3

I had a lot of problems when I tried to use FusedLocationApi from my Xamarin activity. The approach used by the code listed here Location Xamarin has been marked obsolete, so it didn't compile. My implementation is as follows. The question I have is, if this is the way to do it or if I am overlooking something much easier? The LocationHandler is used by my activity, e.g. OnCreate, OnResume, OnPause call the connect and disconnect methods. The OnChangedLocation method should of course do something more intelligent.

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);
            }
        }


    }

}
like image 951
Lars Nielsen Avatar asked Oct 31 '22 20:10

Lars Nielsen


1 Answers

I guess not. I will close the question, but not remove the question as people can then find an example of LocationServices that work.

like image 116
Lars Nielsen Avatar answered Nov 10 '22 00:11

Lars Nielsen