首页 > 解决方案 > 选择地址时,Google Places Autocomplete API 不会返回城市和州

问题描述

我试图让我的 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");



    }

标签: c#androidxamarin.formsgoogle-places-apigoogleplacesautocomplete

解决方案


推荐阅读