xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

Xamarin.Essentials Error Foundation.NSErrorException in iOS Geocoding.GetLocationAsync #673

Closed technololy closed 5 years ago

technololy commented 5 years ago

Bug report best practices: Submitting Issues

Description

Description Geocoding(getting latitude/longitude from an address) works fine on Android but not in iOS (emulator and real device). The same address listed below returns, via geocoding, the latitude and longitude only on Android, not on iOS. I used the document here https://docs.microsoft.com/en-us/xamarin/essentials/. Note: Only particular to iOS

Steps to Reproduce

Steps to reproduce:

  1. Run the project in this url. https://github.com/technololy/TestingGeocoding
  2. While running, enter an address (some e.g. are i) 20a Admiralty Way, Eti-Osa, Lagos, Nigeria, ii)Plot No 3 & 4, Block XVI, Oniru Estate, Victoria Island, Near Landmark Centre iii) Block 26, Plot 10 Admiralty Way, Lekki Phase 1 105102, Lekki
  3. click on the button to search for the longitude and latitude of the address

Expected Behavior

Expected Behavior Returns longitude and latitude of the entered address

Actual Behavior

Enters the catch which gives this error: {Foundation.NSErrorException: Error Domain=kCLErrorDomain Code=8 "(null)" at Xamarin.Essentials.Geocoding+d__4.MoveNext () [0x00075] in <80b8ae4e16374c93b74637a8a9a92836>:0 --- End of stack trace from previous location where exception was thrown

Basic Information

Screenshots

Reproduction Link

Reproduction Link here: https://github.com/technololy/TestingGeocoding

jamesmontemagno commented 5 years ago

We submit this code directly through the iOS API: https://developer.apple.com/documentation/corelocation/clgeocoder/1423509-geocodeaddressstring

There is not additional logic done by Essentials, just looks like iOS can't geocode your address :( Best to try/catch around it.

technololy commented 5 years ago

Thanks. also csme to that conclusion later on

jBijsterboschNL commented 3 years ago

@technololy, what I did was simply creating an extension method for this:


{
    public static async Task<IEnumerable<Location>> SafeGetLocationsAsync(this Geocoding geocoding, string address)
    {
        try
        {
            var location = await geocoding.GetLocationsAsync(address);
            return location;
        }
        catch (Exception)
        {
            //NOTE: because the iOS implementation is throwing an Exception, this helper method is implemented.
            //      For details check: https://github.com/xamarin/Essentials/issues/673
            return new List<Location>();
        }
    }
}