Example #1
0
        /// <summary>
        /// Creates the predictions URI.
        /// </summary>
        /// <returns>The predictions URI.</returns>
        /// <param name="newTextValue">New text value.</param>
        /// <param name="apiKey">The API key</param>
        /// <param name="bias">The location bias (can be NULL)</param>
        /// <param name="components">The components (can be NULL)</param>
        /// <param name="type">Filter for the returning types </param>
        /// <param name="language">The language of the results</param>
        private static string CreatePredictionsUri(string newTextValue, string apiKey, LocationBias bias, Components components, PlaceType type, GoogleAPILanguage language)
        {
            var url   = "https://maps.googleapis.com/maps/api/place/autocomplete/json";
            var input = Uri.EscapeUriString(newTextValue);
            var pType = PlaceTypeValue(type);

            var constructedUrl = $"{url}?input={input}&types={pType}&key={apiKey}";

            if (bias != null)
            {
                constructedUrl = constructedUrl + bias;
            }

            if (components != null)
            {
                constructedUrl += components;
            }

            if (language != GoogleAPILanguage.Unset)
            {
                constructedUrl += "&Language=" + GoogleAPILanguageHelper.ToAPIString(language);
            }

            return(constructedUrl);
        }
Example #2
0
        /// <summary>
        /// Calls the Google Places API to retrieve autofill suggestions
        /// </summary>
        /// <returns>The places.</returns>
        /// <param name="newTextValue">New text value.</param>
        /// <param name="apiKey">The API key</param>
        /// <param name="bias">The location bias (can be NULL)</param>
        /// <param name="components">The components (can be NULL)</param>
        /// <param name="type">Filter for the returning types </param>
        /// <param name="language">The language of the results</param>
        public static async Task <AutoCompleteResult> GetPlaces(string newTextValue, string apiKey, LocationBias bias, Components components, PlaceType type, GoogleAPILanguage language)
        {
            if (string.IsNullOrEmpty(apiKey))
            {
                throw new Exception("You have not assigned a Google API key to PlacesBar");
            }

            try
            {
                var requestURI = CreatePredictionsUri(newTextValue, apiKey, bias, components, type, language);
                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(AutoCompleteResult.FromJson(JObject.Parse(result)));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("PlacesBar HTTP issue: {0} {1}", ex.Message, ex);
                return(null);
            }
        }