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