Ejemplo n.º 1
0
        public static string FullAddressReverseGeocoder(string lat, string lng)
        {
            string      apiKey   = Credentials.GoogleMapsApiKey;
            string      url      = $"https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{lng}&key={apiKey}";
            WebResponse response = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                response       = request.GetResponse();
                if (response != null)
                {
                    string       responseString = null;
                    Stream       stream         = response.GetResponseStream();
                    StreamReader streamReader   = new StreamReader(stream);
                    responseString = streamReader.ReadToEnd();
                    GeoResponse geoResponse = JsonConvert.DeserializeObject <GeoResponse>(responseString);
                    if (geoResponse.status == "OK")
                    {
                        string relevantAddress = geoResponse.results[0].formatted_address;

                        return(relevantAddress);
                    }
                    return("");
                }
                return("");
            }
            catch
            {
                throw new Exception("Google maps was unable to find address");
            }
        }
Ejemplo n.º 2
0
        public static string[] RunGeocoder(string city, string state)
        {
            city  = city.Trim().Replace(" ", "+");
            state = state.Trim();
            string      apiKey   = Credentials.GoogleMapsApiKey;
            string      url      = $"https://maps.googleapis.com/maps/api/geocode/json?components=route:{city}|administrative_area:{state}|country:USA&key={apiKey}";
            WebResponse response = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                response       = request.GetResponse();
                string custLat  = "43.0362012";
                string custLong = "-87.98582829999999";
                if (response != null)
                {
                    string       responseString = null;
                    Stream       stream         = response.GetResponseStream();
                    StreamReader streamReader   = new StreamReader(stream);
                    responseString = streamReader.ReadToEnd();
                    GeoResponse geoResponse = JsonConvert.DeserializeObject <GeoResponse>(responseString);

                    if (geoResponse.status == "OK")
                    {
                        custLat  = geoResponse.results[0].geometry.location.lat.ToString();
                        custLong = geoResponse.results[0].geometry.location.lng.ToString();
                    }
                    return(new string[] { custLat, custLong });
                }
                return(new string[] { custLat, custLong });
            }
            catch
            {
                throw new Exception("Google maps was unable to find address");
            }
        }