Google地方信息自动填充功能不会填充地址

时间:2019-01-26 03:45:55

标签: c# visual-studio xamarin.forms google-places-api

我有一个活跃的googol地方,可以使用Xamarin Forms或Cross Platform自动完成。我有一个可行的解决方案,当用户键入地址时,该地址会自动填充地址。我的问题是,当用户从列表中选择该地址时,该地址不会转到search_bar.text…。搜索栏仅保留键入的文本。选择要填充到搜索栏中的文本时如何获取文本。

仅供参考,我是Xamarin表单和C#的新手,所以请尽可能具体。

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

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

    } 

2 个答案:

答案 0 :(得分:1)

在ItemSelected方法中,您需要设置搜索栏的文本:

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

        var prediction = (AutoCompletePrediction)e.SelectedItem;

        search_bar.Text = prediction.Name? // Your property here

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

    } 

答案 1 :(得分:0)

我仍在尝试解决此问题,它仅添加街道名称和号码,而不是整个地址