public async Task GeocodeAsync(double latitude, double longitude)
        {
            // Provide interim information
            _geocodeResult = new GeocodeResult()
                                 {
                                     Latitude = latitude,
                                     Longitude = longitude,
                                     Name = String.Format("Lat: {0}, Lon: {1}", latitude, longitude)
                                 };
            SetQueryResultToGeocodeResult(QueryResult);

            try
            {
                var geocodeProxy = CreateGeocodeProxy();
                var result = await geocodeProxy.ReverseGeocode(latitude, longitude);

                _geocodeResult = result;
                SetQueryResultToGeocodeResult(QueryResult);
            }
            catch (Exception)
            {
                ErrorService.ShowLightDismissError("Ihr Ort konnte nicht erfolgreich erkannt werden");
            }
        }
Ejemplo n.º 2
0
        // 
        // Copy/Paste Sample: http://open.mapquestapi.com/nominatim/v1/reverse?format=xml&lat=51.521435&lon=-0.162714
        //
        public async Task<GeocodeResult> ReverseGeocode(double latitude, double longitude)
        {
            string URL =
                String.Format(
                    "http://open.mapquestapi.com/nominatim/v1/reverse?format=xml&lat={0}&lon={1}",
                    latitude.ToString(CultureInfo.InvariantCulture),
                    longitude.ToString(CultureInfo.InvariantCulture));

            string response = "";
            using (var client = new HttpClient())
            {
                response = await client.GetStringAsync(URL);
                client.Dispose();
            }

            var searchresults = XElement.Parse(response);
            var resElement = searchresults.Elements("result").FirstOrDefault();
            var addressElement = searchresults.Elements("addressparts").FirstOrDefault();

            if (resElement != null && addressElement != null)
            {
                var countryCode = (string)addressElement.Element("country_code");

                if (0 == String.Compare("at", countryCode, StringComparison.OrdinalIgnoreCase))
                {
                    // Only if City or Town is not available, we will fall back to the actual location name
                    string locationName = (string)addressElement.Element("city");
                    if (String.IsNullOrWhiteSpace(locationName))
                    {
                        locationName = (string)addressElement.Element("town");

                        if (String.IsNullOrWhiteSpace(locationName))
                        {
                            locationName = (string)resElement;
                        }
                    }

                    var result = new GeocodeResult()
                                     {
                                         Name = locationName,
                                         Latitude = MappingHelpers.ConvertDouble((string) resElement.Attribute("lat")),
                                         Longitude = MappingHelpers.ConvertDouble((string) resElement.Attribute("lon"))
                                     };
                    
                    return result;
                }
            }

            return null;
        }