选择地址后,Google Places Autocomplete API不会返回城市和州

时间:2019-01-30 22:45:16

标签: c# android xamarin.forms google-places-api googleplacesautocomplete

我试图让我的search_bar填充整个地址(街道名称和数字,城市,州)。.现在,选择地址后,只有街道名称和数字会填充在搜索栏中。

   [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Createbusinessaccount : ContentPage
{

    string GooglePlacesApiKey = "I removed this";  // Replace this with your api key

    public Createbusinessaccount ()
    {
        InitializeComponent ();

        search_bar.ApiKey = GooglePlacesApiKey;
        search_bar.Type = PlaceType.Address;
        search_bar.Components = new Components("country:us"); // Restrict results to Australia and New Zealand
        search_bar.PlacesRetrieved += Search_Bar_PlacesRetrieved;
        search_bar.TextChanged += Search_Bar_TextChanged;
        search_bar.MinimumSearchText = 2;
        results_list.ItemSelected += Results_List_ItemSelected;

    }



    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);

        search_bar.Text = place.Name;

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



    }

0 个答案:

没有答案