コード例 #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));
                }
            }
        }
        /// <summary>
        /// Calls Google places API
        /// </summary>
        /// <param name="query">Search term to query</param>
        /// <param name="coordinate">Coordinate to bias the result</param>
        /// <param name="radius">Radius to bias the result</param>
        /// <param name="status">Response status</param>
        /// <returns>List of Google Places</returns>
        ///
        public List <GooglePlace> Search(string query, GeoCoordinate coordinate, long?radius, out GoogleResponseStatus status)
        {
            string location = (coordinate != null) ? String.Format("{0},{1}", coordinate.Latitude, coordinate.Longitude) : null;
            var    uri      = String.Format(MapUri, query, ApiKey, Convert.ToString(radius), location);

            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(GooglePlaceResult.ProcessResult(json, out status));
                }
            }
        }