コード例 #1
0
ファイル: Places.cs プロジェクト: congtrongkttv/Bookcar
        /// <summary>
        /// Gets the place.
        /// </summary>
        /// <returns>The place.</returns>
        /// <param name="placeID">Place identifier.</param>
        /// <param name="apiKey">API key.</param>
        /// <param name="fields">The fields to query (see https://developers.google.com/places/web-service/details#fields )</param>
        public static async Task <Place> GetPlace(string placeID, string apiKey, PlacesFieldList fields = null)
        {
            fields = fields ?? PlacesFieldList.ALL;             // default = ALL fields

            try
            {
                var requestURI = CreateDetailsRequestUri(placeID, apiKey, fields);
                var client     = new HttpClient();
                var request    = new HttpRequestMessage(HttpMethod.Get, requestURI);
                var response   = await client.SendAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    Debug.WriteLine("PlacesBar HTTP request denied.");
                    return(null);
                }

                var result = await response.Content.ReadAsStringAsync();

                if (result == "ERROR")
                {
                    Debug.WriteLine("PlacesSearchBar Google Places API returned ERROR");
                    return(null);
                }

                return(new Place(JObject.Parse(result)));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("PlacesBar HTTP issue: {0} {1}", ex.Message, ex);
                return(null);
            }
        }
コード例 #2
0
ファイル: Places.cs プロジェクト: congtrongkttv/Bookcar
        /// <summary>
        /// Creates the details request URI.
        /// </summary>
        /// <returns>The details request URI.</returns>
        /// <param name="place_id">Place identifier.</param>
        /// <param name="apiKey">API key.</param>
        /// <param name="fields">The fields to query (see https://developers.google.com/places/web-service/details#fields )</param>
        private static string CreateDetailsRequestUri(string place_id, string apiKey, PlacesFieldList fields)
        {
            var url = "https://maps.googleapis.com/maps/api/place/details/json";

            url += $"?placeid={Uri.EscapeUriString(place_id)}";
            url += $"&key={apiKey}";
            if (!fields.IsEmpty())
            {
                url += $"&fields={fields}";
            }
            return(url);
        }