title: Using Android Location Services in a XamarinForms App
body: <p>I have a Xamarin Forms Application. Fairly basic setup with a PCL containing some XAML Pages.</p>
  
  <p>I'm trying to get a very simple "display my gps co-ordinates" sample running based on a button click in my Xaml Page.</p>
  
  <p>I've created an ILocationService interface in my PCL, and an android specific implementation in my Android app, tagged with the <code>[assembly:Dependency]</code> attribute so I can fetch it with the XamForms DI Service.</p>
  
  <p>Here's the code for the Android Implementation.</p>
  
  <pre><code>[assembly: Dependency(typeof(AndroidLocationService))]
  
  namespace LoginDemo.Droid
  {
      public class AndroidLocationService : ILocationService, ILocationListener
      {
          Location _currentLocation;
          LocationManager _locationManager;
          String _locationProvider;
  
          public AndroidLocationService()
          {
              this.InitializeLocationManager();
          }
  
          //When the DI Container fetches a new instance of the AndroidLocationService
          //Wire up the _locationManager/Provider stuff and tell it to start fetching updates every 2 ses
          void InitializeLocationManager()
          {
              _locationManager = (LocationManager)Forms.Context.GetSystemService(Context.LocationService);
              Criteria criteriaForLocationService = new Criteria { Accuracy = Accuracy.Fine };
              try
              {
                  _locationProvider = _locationManager.GetBestProvider(criteriaForLocationService, true);
                  if (_locationManager.IsProviderEnabled(_locationProvider))
                  {
                      _locationManager.RequestLocationUpdates(_locationProvider, 2000, 1, this);
                  }
              }
              catch (Exception ex)
              {
                  System.Diagnostics.Debug.WriteLine(ex);
              }
          }
  
          //MyInterface Method
          public LocationResult GetLocation()
          {
  
              if (_currentLocation == null)
              {
                  return new LocationResult() { Data = "Can't determine the current address." };
              }
  
              return new LocationResult { Data = string.Format("{0} - {1}", _currentLocation.Latitude, _currentLocation.Longitude)};
  
          }
  
          public void Dispose()
          {
          }
  
          public IntPtr Handle { get; private set; }
  
          public void OnLocationChanged(Location location)
          {
              _currentLocation = location;
          }
  
          public void OnProviderDisabled(string provider)
          {
              //Not Implemented
          }
  
          public void OnProviderEnabled(string provider)
          {
              //Not Implemented
          }
  
          public void OnStatusChanged(string provider, Availability status, Bundle extras)
          {
              //Not Implemented
          }
      }
  }
  </code></pre>
  
  <p><code>_locationProvider</code> gets set to the value <code>gps</code> by the <code>GetBestProvider</code> method, and it passes the <code>isEnabled</code> check, but as soon as it attempts to request updates... I get the following (rather unhelpful) Exception. I've double checked the app manifest to make sure the permissions are turned on for course &amp; fine location requests. Any ideas ?</p>
  
  <pre><code>{Java.Lang.IllegalArgumentException: Exception of type 'Java.Lang.IllegalArgumentException' was thrown.
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in &lt;filename unknown&gt;:0 
    at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00063] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:508 
    at Android.Locations.LocationManager.RequestLocationUpdates (System.String provider, Int64 minTime, Single minDistance, ILocationListener listener) [0x0004a] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Locations.LocationManager.cs:814 
    at LoginDemo.Droid.AndroidLocationService.InitializeLocationManager () [0x00058] in c:\Work\Code\Sandbox\LoginDemo\LoginDemo\LoginDemo.Android\LocationService.cs:46 
    --- End of managed exception stack trace ---
  java.lang.IllegalArgumentException: invalid listener: null
      at android.location.LocationManager.checkListener(LocationManager.java:1606)
      at android.location.LocationManager.requestLocationUpdates(LocationManager.java:426)
      at xamarin.forms.platform.android.ButtonRenderer_ButtonClickListener.n_onClick(Native Method)
      at xamarin.forms.platform.android.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:29)
      at android.view.View.performClick(View.java:4247)
      at android.view.View$PerformClick.run(View.java:17728)
      at android.os.Handler.handleCallback(Handler.java:730)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5289)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:525)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
      at dalvik.system.NativeStart.main(Native Method)
  }
  </code></pre>