public async override Task<Location> GetLocation() { var ret = new Location(); // create the locator Geolocator geolocator = new Geolocator(); geolocator.DesiredAccuracyInMeters = 50; try { Geoposition geoposition = await geolocator.GetGeopositionAsync( maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(10) ); // THIS IS THE ONLY DIFFERENCE ret.Latitude = geoposition.Coordinate.Point.Position.Latitude; ret.Longitude = geoposition.Coordinate.Point.Position.Longitude; } catch (Exception ex) { if ((uint)ex.HResult == 0x80004004) { // the application does not have the right capability or the location master switch is off } else { // something else happened acquring the location } } // return the location return ret; }
public override Task<Location> GetLocation() { manager.StartUpdatingLocation(); return Task.Factory.StartNew<Location>(() => { var ret = new Location(); var locationFound = false; // create the locator manager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => { if (e.Locations.Length > 0) { ret = new Location() { Latitude = e.Locations[0].Coordinate.Latitude, Longitude = e.Locations[0].Coordinate.Longitude }; } locationFound = true; }; manager.Failed += (object sender, NSErrorEventArgs e) => { locationFound = true; }; // wait till done while (!locationFound) Task.Delay(200); // stop udpating the location manager.StopUpdatingLocation(); // return the location return ret; }); }