Exemple #1
0
        private static Location _getLocationFromJson(GoogleGeoCodingResponse googleResponse)
        {
            try
            {
                Location loc = new Location();

                results res = googleResponse.results[0];
                loc.Latitude  = Convert.ToDouble(res.geometry.location.lat, System.Globalization.CultureInfo.InvariantCulture);
                loc.Longitude = Convert.ToDouble(res.geometry.location.lng, System.Globalization.CultureInfo.InvariantCulture);

                foreach (address_component adress in res.address_components)
                {
                    if (adress.types.Contains("street_number"))
                    {
                        loc.HouseNumber = adress.long_name;
                    }
                    else if (adress.types.Contains("route"))
                    {
                        loc.StreetName = adress.long_name;
                    }
                    else if (adress.types.Contains("locality"))
                    {
                        loc.CityName = adress.long_name;
                    }
                    else if (adress.types.Contains("country"))
                    {
                        loc.CountryName = adress.long_name;
                    }
                    else if (adress.types.Contains("postal_code"))
                    {
                        loc.Zipcode = adress.long_name;
                    }
                }
                return(loc);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemple #2
0
        private static Location _executeResponseMessage(HttpResponseMessage message)
        {
            String response = message.Content.ReadAsStringAsync().Result;

            //Begin with json parsing
            try
            {
                if (response.StartsWith("["))
                {
                    response = response.Remove(0, 1);
                }
                if (response.EndsWith("]"))
                {
                    response = response.Remove((response.Length - 1), 1);
                }

                GoogleGeoCodingResponse googleResponse = JsonConvert.DeserializeObject <GoogleGeoCodingResponse>(response);

                if (googleResponse.status == "ZERO_RESULTS")
                {
                    return(null);
                }
                else if (googleResponse.status == "OK")
                {
                    Location loc = _getLocationFromJson(googleResponse);
                    return(loc);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                //Something went wrong during the parsing process
                return(null);
            }
        }