Пример #1
0
        /// <summary>
        /// Geocode an address to lat, lng
        /// </summary>
        /// <param name="byComponent">If true filter results so they only match each component filter set</param>
        /// <param name="delay">If looping, set a short delay to avoid being rate limited. Delay is in milliseconds. 50 should work.</param>
        /// <returns></returns>
        public List <GoogleAddress> Search(bool byComponent = false, int delay = 0)
        {
            if (delay != 0)
            {
                Thread.Sleep(delay);
            }

            var uri = String.Format(MapUri, UseHttps ? "https" : "http", GetComponentFilter(byComponent));

            uri = GoogleRequestHelper.SetQuotaUser(uri, this.QuotaUser);
            var httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

            httpRequest.ContentType = "application/json; charset=utf-8";
            httpRequest.Method      = WebRequestMethods.Http.Get;
            httpRequest.Accept      = "application/json";

            using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
            {
                using (var sr = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var json = sr.ReadToEnd();
                    return(GoogleAddress.FromResults(json));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Finds a Google Address based on GeoLocation (latitude, longitude)
        /// </summary>
        /// <param name="latitude">latitude</param>
        /// <param name="longitude">longitude</param>
        /// <returns>List of matching addresses</returns>
        public List <GoogleAddress> Search(double latitude, double longitude)
        {
            var uri         = String.Format(MapUri, latitude, longitude, ApiKey);
            var httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

            httpRequest.ContentType = "application/json; charset=utf-8";
            httpRequest.Method      = WebRequestMethods.Http.Get;
            httpRequest.Accept      = "application/json";

            using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
            {
                using (var sr = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var json = sr.ReadToEnd();
                    return(GoogleAddress.FromResults(json));
                }
            }
        }