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! :)

5 Replies

MK Muneesh Kumar G Syncfusion Team July 22, 2019 12:45 PM UTC

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 10:48 AM UTC

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 09:54 AM UTC

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   
 



MA Matthew October 14, 2022 11:56 PM UTC

Hello,


I found this thread and am having issues with the code provided in the demo. 

It looks like the code is working up to and including the Filter.js function. I can see the queries in the console of my browser and the autocomplete data being returned (picture attached). 

However, the data being returned in the JSRuntime.InvokeAsync call is coming back as null (see attached picture).

Any suggestions?


Thanks!



Attachment: Screenshots_1a864b56.zip


RS Ruba Shanmugam Syncfusion Team October 19, 2022 03:59 AM UTC

Hi Matthew,


Query: However, the data being returned in the JSRuntime.InvokeAsync call is coming back as null (see attached picture)

We have checked the reported query and we don’t get your exact requirement because already we provided sample in Xamarin.Forms. But based on your last update screenshot, it’s related to the Blazor application.

Could you please confirm the issue details and platform details to proceed further on this?


Regards,

Ruba Shanmugam


Loader.
Up arrow icon