title: Xamarin.Forms DependencyService difficulty getting location
body: <p>So after finally getting my head around Xamarin.Forms DependencyService I have nearly got it returning the device's current location.</p>
  
  <p>my interface</p>
  
  <pre><code>public interface ICurrentLocation
  {
      MyLocation SetCurrentLocation();
  }
  </code></pre>
  
  <p>MyLocation</p>
  
  <pre><code>public class MyLocation
  {
      public double Latitude {get; set;}
      public double Longitude{get; set;}
  }
  </code></pre>
  
  <p>the line that calls it</p>
  
  <pre><code>MyLocation location = DependencyService.Get&lt;ICurrentLocation&gt;().SetCurrentLocation();
  </code></pre>
  
  <p>and in the CurrentLocation class in the Android project that implements the Geolocation class of Xamarin.Mobile</p>
  
  <pre><code>[assembly: Dependency(typeof(CurrentLocation))]
  namespace MyCockburn.Droid
  {
  
      public class CurrentLocation : Activity, ICurrentLocation
  
          Geolocator locator;
          Position position = new Position();
          MyLocation location;
  
          public MyLocation SetCurrentLocation()
          {
              GetPosition();
              location = new MyLocation()
              {
                  Latitude = position.Latitude,
                  Longitude = position.Longitude
              };
  
  
              return location;
         }
  
         async void GetPosition()
         {
             try
             {
                   locator = new Geolocator(this) { DesiredAccuracy = 50 };
                   if (locator.IsListening != true)
                         locator.StartListening(minTime: 1000, minDistance: 0);
  
                   position = await locator.GetPositionAsync(timeout: 20000);
  
             }
             catch (Exception e)
             {
                 Log.Debug("GeolocatorError", e.ToString());
             }
         }
     }
  </code></pre>
  
  <p>my problem seems to be that is returning location before position holds the longitude and latitude</p>
  
  <p>I am hoping my mistake is glaringly obvious</p>
  
  <p>EDIT: the code works if I run it as a normal Android Activity</p>