Xamarin google maps api

I’m using the code below to get the city name of a given Latitude and Longitude.

But the code stops when this line is running:

var responseElement = XElement.Parse(response);

It stops responding and doesn’t throw any exception.

I’m guessing it may be a problem with the asynchronous call.

Can anyone help me with this, or suggest a similar code that use the iOS SDK? Thanks

    private async Task<string> ReturnCity(string lat, string lon)
    {
        string city = "None";
        string baseUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", lat, lon);
        HttpClient client = new HttpClient();
        var response = await client.GetStringAsync(baseUri);
        var responseElement = XElement.Parse(response);
        IEnumerable<XElement>statusElement = from st in responseElement.Elements("status") select st;
        if (statusElement.FirstOrDefault() != null)
        {
            string status = statusElement.FirstOrDefault().Value;
            if (status.ToLower() == "ok")
            {
                IEnumerable<XElement> resultElement = from rs in responseElement.Elements("result") select rs;
                if (resultElement.FirstOrDefault() != null)
                {
                    IEnumerable<XElement> addressElement = from ad in resultElement.FirstOrDefault().Elements("address_component") select ad;
                    foreach (XElement element in addressElement)
                    {
                        IEnumerable<XElement> typeElement = from te in element.Elements("type") select te;
                        string type = typeElement.FirstOrDefault().Value;
                        if(type=="locality")
                        {
                            IEnumerable<XElement> cityElement = from ln in element.Elements("long_name") select ln;
                            city = cityElement.FirstOrDefault().Value;
                            return city;
                            break;
                        }
                    }
                }
            }
        }
        return city;
    }


Source: ios

Leave a Reply