We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Autocomplete Google Places

Hey there!

I'm trying to achieve a situation where I search for results only for a SPECIFIC COUNTRY.
I tried to accomplish that with requests and it DOES WORK, though lagging and buggy. I'm not using any MVVM pattern.
So, the current problems are:
1.  If I write the address too fast, it won't do anything and the list will stay empty
2. Result is not restricted to a specific country
3. Too slow - I think it's because there is no delay between requests

Code:
XAML:
            <autocomplete:SfAutoComplete
                x:Name="placesAutoComplete"
                Grid.Row="3"
                AutoCompleteMode="SuggestAppend"
                PropertyChanging="PlacesAutoComplete_OnPropertyChanging" />
C#:
        private void PlacesAutoComplete_OnPropertyChanging(object sender, PropertyChangingEventArgs e)
        {
            Device.BeginInvokeOnMainThread(async() =>
            {
                try
                {
                    placesAutoComplete.AutoCompleteSource =
                        (List<string>) await MapHandler.GetPlacesAutocompleteAsync((sender as SfAutoComplete)?.Text);
                }
                catch (Exception err)
                {
                    System.Diagnostics.Debug.WriteLine("Exception with autocomplete: " + err.Message + " stack :" + err.StackTrace);
                }
            });
        }

The call for API:
        public static async Task<IEnumerable<string>> GetPlacesAutocompleteAsync(string search)
        {
            var request = string.Format("https://maps.googleapis.com/maps/api/place/autocomplete/xml?input={0}&key={1}",
                search, "APIKEY");
            HttpClient client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
            var xml = await client.GetStringAsync(request);
            var results = XDocument.Parse(xml).Element("AutocompletionResponse")?.Elements("prediction");

            var suggestions = new List<string>();
            if (results != null)
                foreach (var result in results)
                {
                    var suggestion = result.Element("description")?.Value;
                    suggestions.Add(suggestion);
                }

            return suggestions;
        }

How should I go with improving this code?

Thanks in advance! :)

3 Replies

MK Muneesh Kumar G Syncfusion Team July 22, 2019 07:45 AM

Hi Aviad, 
 
Greetings from Syncfusion.  
 
We have checked the provided code snippet and we would like to let you know that the PropertyChanged getting invoked every time and if we need to achieve the requirement please use the ValueChanged, SelectionChanged Event for the purpose required. 
 
 
Please check with the sample and let us know if you have any other requirement. 
 
Regards,
Muneesh Kumar G. 
 
 



AK Aviad Katani replied to Muneesh Kumar G August 27, 2019 05:48 AM

Hi Aviad, 
 
Greetings from Syncfusion.  
 
We have checked the provided code snippet and we would like to let you know that the PropertyChanged getting invoked every time and if we need to achieve the requirement please use the ValueChanged, SelectionChanged Event for the purpose required. 
 
 
Please check with the sample and let us know if you have any other requirement. 
 
Regards,
Muneesh Kumar G. 
 
 


Thank you, it is working. Though app crashes sometimes when a long addrss is displayed:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.



MK Muneesh Kumar G Syncfusion Team August 28, 2019 04:54 AM

Hi Aviad, 
 
We have checked with the long address in SfAutoComplete control and unfortunately, we could not able to reproduce the same.  
 
And if the issue is still persisting, please mention the RestUrl and platform details and also if possible, please provide the issue reproducing sample which will be helpful for us to analyze the issue and provide an appropriate solution. 
 
Please let us know if you have any concern. 
  
Regards,   
Muneesh Kumar G   
 


Loader.
Live Chat Icon For mobile
Up arrow icon