public static AutoCompleteResult FromJson(JObject result)
        {
            var r = new AutoCompleteResult();

            r.Status = result["status"].Value <string>();

            r.AutoCompletePlaces = new List <AutoCompletePrediction>();
            foreach (var obj in result["predictions"].Value <JArray>())
            {
                r.AutoCompletePlaces.Add(AutoCompletePrediction.FromJson(obj.Value <JObject>()));
            }

            return(r);
        }
示例#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);
            }
        }
示例#3
0
        protected virtual void OnPlacesRetrieved(AutoCompleteResult e)
        {
            PlacesRetrievedEventHandler handler = PlacesRetrieved;

            handler?.Invoke(this, e);
        }