title: Using FusedLocationApi with Xamarin 3
body: <p>I had a lot of problems when I tried to use <em>FusedLocationApi</em> from my Xamarin activity. The approach used by the code listed here <a href="http://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/">Location Xamarin</a> 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.</p>
  
  <pre><code>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 &amp;&amp; _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);
              }
          }
  
  
      }
  
  }
  </code></pre>