Esempio n. 1
0
        /// <summary>
        /// Retrieve address details for a specified set of coordinates.
        /// </summary>
        /// <param name="latitude">Latitude.</param>
        /// <param name="longitude">Longitude.</param>
        /// <returns>Address details.</returns>
        public Address QueryCoordinates(double latitude, double longitude)
        {
            string url = _BaseUrl + _ApiKey + "&latlng=" + latitude + "," + longitude;

            Logger?.Invoke(_Header + "QueryCoordinates " + latitude + "," + longitude);
            GoogleMapsResponse resp = GetGoogleMapsResponse(url);

            return(new Address(resp));
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve address details for a specified address.
        /// </summary>
        /// <param name="address">Address.</param>
        /// <returns>Address details.</returns>
        public Address QueryAddress(string address)
        {
            if (String.IsNullOrEmpty(address))
            {
                throw new ArgumentNullException(nameof(address));
            }
            string url = _BaseUrl + _ApiKey + "&address=" + WebUtility.UrlEncode(address);

            Logger?.Invoke(_Header + "QueryAddress " + address);
            GoogleMapsResponse resp = GetGoogleMapsResponse(url);

            return(new Address(resp));
        }
Esempio n. 3
0
        /// <summary>
        /// Instantiate the response.
        /// </summary>
        /// <param name="resp">Google Maps response.</param>
        public Address(GoogleMapsResponse resp)
        {
            if (resp == null)
            {
                throw new ArgumentNullException(nameof(resp));
            }

            _GoogleMapsResponse = resp;

            if (resp.Status.Equals("OK") &&
                resp.Results != null &&
                resp.Results.Count > 0)
            {
                GoogleMapsResponse.Result result = resp.Results[0];

                FormattedAddress = result.FormattedAddress;

                if (result.AddressComponents != null &&
                    result.AddressComponents.Count > 0)
                {
                    foreach (GoogleMapsResponse.Result.AddressComponent comp in result.AddressComponents)
                    {
                        if (comp.Types != null && comp.Types.Count > 0)
                        {
                            foreach (string currType in comp.Types)
                            {
                                if (currType.Equals("street_number"))
                                {
                                    StreetNumber = comp.LongName;
                                }

                                if (currType.Equals("route"))
                                {
                                    StreetName = comp.LongName;
                                }

                                if (currType.Equals("neighborhood"))
                                {
                                    Neighborhood = comp.LongName;
                                }

                                if (currType.Equals("locality"))
                                {
                                    City = comp.LongName;
                                }

                                if (currType.Equals("administrative_area_level_2"))
                                {
                                    County = comp.LongName;
                                }

                                if (currType.Equals("administrative_area_level_1"))
                                {
                                    State            = comp.LongName;
                                    StateAbbreviated = comp.ShortName;
                                }

                                if (currType.Equals("country"))
                                {
                                    Country            = comp.LongName;
                                    CountryAbbreviated = comp.ShortName;
                                }

                                if (currType.Equals("postal_code"))
                                {
                                    Postal = comp.LongName;
                                }

                                if (currType.Equals("postal_code_suffix"))
                                {
                                    PostalSuffix = comp.LongName;
                                }
                            }
                        }
                    }
                }

                if (result.Geometry != null)
                {
                    if (result.Geometry.Bounds != null)
                    {
                        if (result.Geometry.Bounds.Northeast != null)
                        {
                            NortheastBoundary           = new Coordinates();
                            NortheastBoundary.Latitude  = result.Geometry.Bounds.Northeast.Latitude;
                            NortheastBoundary.Longitude = result.Geometry.Bounds.Northeast.Longitude;
                        }

                        if (result.Geometry.Bounds.Southwest != null)
                        {
                            SouthwestBoundary           = new Coordinates();
                            SouthwestBoundary.Latitude  = result.Geometry.Bounds.Southwest.Latitude;
                            SouthwestBoundary.Longitude = result.Geometry.Bounds.Southwest.Longitude;
                        }
                    }

                    if (result.Geometry.Location != null)
                    {
                        Latitude  = result.Geometry.Location.Latitude;
                        Longitude = result.Geometry.Location.Longitude;
                    }
                }
            }
        }