/// <summary> /// Calls Google places API /// </summary> /// <param name="query">Search term to query</param> /// <returns>List of Google Places</returns> public List <GooglePlace> Search(string query, out GoogleResponseStatus status) { return(this.Search(query, null, null, out status)); }
/// <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)); } } }
//public enum GooglePlaceStatus //{ // OK = 1, // REQUEST_DENIED //} public static List <GooglePlace> ProcessResult(string googlePlaceResultJson, out GoogleResponseStatus status) { var googlePlaceResult = googlePlaceResultJson.FromJson <GooglePlaceResult>(); var result = new List <GooglePlace>(); foreach (var place in googlePlaceResult.candidates) { result.Add(new GooglePlace { Name = place.name, Address = place.formatted_address, Location = new GeoCoordinate(place.geometry.latitude, place.geometry.longitude), PlaceId = place.place_id }); } status = googlePlaceResult; return(result); }