Forum Xamarin.Forms

Google Places Autocomplete does not populate the address

I have an active googol places autocomplete working with Xamarin Forms or Cross Platform. I have a working solution that auto populates the address when the user types in the address. My problem is when the user selects it from the list the address does not go to the search_bar.text… The search bar just remains with the text that was typed.? how can I get the text when selected to populate in the search bar.
FYI I am new to Xamarin forms and C# so pls be specific as possible thx.

void Search_Bar_PlacesRetrieved(object sender, AutoCompleteResult result)
{
    results_list.ItemsSource = result.AutoCompletePlaces;
    spinner.IsRunning = false;
    spinner.IsVisible = false;

    if (result.AutoCompletePlaces != null && result.AutoCompletePlaces.Count > 0)
        results_list.IsVisible = true;
}

void Search_Bar_TextChanged(object sender, TextChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.NewTextValue))
    {
        results_list.IsVisible = false;
        spinner.IsVisible = true;
        spinner.IsRunning = true;
    }
    else
    {
        results_list.IsVisible = true;
        spinner.IsRunning = false;
        spinner.IsVisible = false;
    }
}

async void Results_List_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    if (e.SelectedItem == null)
        return;

    var prediction = (AutoCompletePrediction)e.SelectedItem;
    results_list.SelectedItem = null;

    var place = await Places.GetPlace(prediction.Place_ID, GooglePlacesApiKey);

    if (place != null)
        await DisplayAlert(
            place.Name, string.Format("Lat: {0}\nLon: {1}", place.Latitude, place.Longitude), "OK");

} 

Best Answer

Answers

  • RittenRARittenRA Member ✭✭

    It Works but it only returns the street name and number not the city and state

  • RittenRARittenRA Member ✭✭

    @LandLu

    5:07AM
    It Works but it only returns the street name and number not the city and state

    5:07AM
    It Works but it only returns the street name and number not the city and state

  • LandLuLandLu Member, Xamarin Team Xamurai

    I guess maybe you used this library to construct your app: https://github.com/ajsmithsw/PlacesSearchBar.
    It only lists Name, Latitude, Longitude, and Raw in Place model:

    public Place(JObject jsonObject)
    {
        Name = (string) jsonObject["result"]["name"];
        Latitude = (double)jsonObject["result"]["geometry"]["location"]["lat"];
        Longitude = (double)jsonObject["result"]["geometry"]["location"]["lng"];
        Raw = jsonObject.ToString();
    }
    

    If you want to show more information, try to refer to this documentation to see what Google can give us: https://developers.google.com/places/web-service/details.
    I think what you need is address_components. You need to customize this library: add more properties you need in that model and parse them there.

  • RittenRARittenRA Member ✭✭

    @LandLu

    I am very new to xamarin forms and im using c#... could you help me out by writing an example using address_components?

Sign In or Register to comment.