Problem in GetPlacemarksAsync

Jassim Al Rahma 1,516 Reputation points
2021-04-10T19:38:52.077+00:00

Hi,

I am trying to get the location from my ContentView in my app.

async void GetLocation()
{
    try
    {
        var request = new GeolocationRequest(GeolocationAccuracy.Default);

        var location = await Geolocation.GetLastKnownLocationAsync();

        if (location == null)
        {
            location = await Geolocation.GetLocationAsync(request);
        }

        latitude = location.Latitude;
        longitude = location.Longitude;

        **var placemarks = await Geocoding.GetPlacemarksAsync(latitude, longitude);**
        var placemark = placemarks?.FirstOrDefault();

        if (placemark != null)
        {
            country_code = placemark.CountryCode;
            country_name = placemark.CountryName;
        }
    }
    catch (FeatureNotSupportedException ex)
    {
        Crashes.TrackError(ex, new Dictionary<string, string>
        {
            { "Page", "HomePage" },
            { "Where", "GetLocation" }
        });
    }
    catch (Exception ex)
    {
        Crashes.TrackError(ex, new Dictionary<string, string>
        {
            { "Page", "HomePage" },
            { "Where", "GetLocation" }
        });
    }
}

but the app is reaching to await Geocoding.GetPlacemarksAsync the existing to the next code although it returns the correct lat and long.

How can I fix this please?

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,287 questions
{count} votes