Пример #1
0
 private static GoogleReverseGeocodeAddressComponent TryGetComponent(GoogleReverseGeocodeResult result, string type)
 {
     return(result
            .Results[0]
            .Address_Components
            .Where(x => x.Types.Any(t => t.Equals(type)))
            .FirstOrDefault());
 }
Пример #2
0
        public static Location ReverseGeocode(Position position)
        {
            UriBuilder uri = new UriBuilder("https://maps.googleapis.com/maps/api/geocode/json");

            uri.Query = string.Format(
                "latlng={0},{1}&key={2}",
                position.Latitude,
                position.Longitude,
                WewyConstants.GOOGLE_MAPS_API_KEY);
            WebRequest request = WebRequest.Create(uri.Uri);
            // Get the response.
            WebResponse response = request.GetResponse();

            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();

            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams and the response.
            reader.Close();
            response.Close();

            JavaScriptSerializer       ser    = new JavaScriptSerializer();
            GoogleReverseGeocodeResult result = ser.Deserialize <GoogleReverseGeocodeResult>(responseFromServer);

            if (result.Status == null || !result.Status.Equals("OK"))
            {
                // Something went wrong. TODO: Log it somewhere!
                // Just return null and we won't store the city name but everything else will work.
                return(null);
            }

            if (result.Results.Count == 0 ||
                result.Results[0].Address_Components == null)
            {
                // TODO: Also log somewhere.
                return(null);
            }

            GoogleReverseGeocodeAddressComponent cityComponent = TryGetComponent(result, "locality");;

            if (cityComponent == null)
            {
                cityComponent = TryGetComponent(result, "administrative_area_level_3");
            }

            if (cityComponent == null)
            {
                cityComponent = TryGetComponent(result, "administrative_area_level_2");
            }

            if (cityComponent == null)
            {
                cityComponent = TryGetComponent(result, "administrative_area_level_1");
            }

            string city = null;

            if (cityComponent != null)
            {
                city = cityComponent.Long_Name;
            }

            GoogleReverseGeocodeAddressComponent countryComponent = TryGetComponent(result, "country");

            string country = null;

            if (countryComponent != null)
            {
                country = countryComponent.Long_Name;
            }

            if (string.IsNullOrEmpty(country) && string.IsNullOrEmpty(city))
            {
                // Don't bother with constructing location object.
                return(null);
            }

            return(new Location()
            {
                City = city,
                Country = country
            });
        }