Search code examples
iosdictionaryxamarinmkannotationviewrenderer

How to obtain a MKPlacemark Address for a callout c# xamarin


I'm trying to get the addresses. All I have so far in getting the coordinates using

 CLLocation loc = new CLLocation(anno.Coordinate.Latitude, anno.Coordinate.Longitude);

However, I tried the reverse geocode and placemark. None of them works. The following example is not finish and it is not proper formatted. I'm just copying from swift.

 async void ReverseGeocodeToConsoleAsync(CLLocation location)
                {
                    var geoCoder = new CLGeocoder();
                    var placemarks = await 
                    geoCoder.ReverseGeocodeLocationAsync(location);
                    var placemark = placemarks[0] as CLPlacemark;
                    var addressString = "";
                    //String to hold address
                        var locatedAtcountry = placemark.country;
                        var locatedAtcity = placemark.locality;
                        var locatedAtisocountry = placemark.ISOcountryCode;
                if (placemark.ISOcountryCode == "TW") /*Address Format in Chinese*/
                {
                    if (placemark.country != null)
                    {
                        addressString = placemark.country!
                    }
                } 

Solution

  • It looks like your code is close to success. Here's a sample which can get the address from the location.

            public void GetAddress(CLLocation loc)
            {
                CLGeocoder ceo = new CLGeocoder();
    
                ceo.ReverseGeocodeLocation(loc, (placemarks, error) =>
                {
                    CLPlacemark placemark = placemarks[0];
                    if (placemark != null)
                    {
                        Console.WriteLine(placemark.IsoCountryCode);
                        Console.WriteLine(placemark.Country);
                    }
                });
            }