placeautocompletefragment完整地址 [英] placeautocompletefragment full address

查看:59
本文介绍了placeautocompletefragment完整地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Google Places API的placeautocompletefragment获取显示在搜索视图中的完整地址?它仅显示整个地址的第一部分.我确信一切都正确实现,并且完整地址由place.getAddress()返回.知道为什么完整地址没有显示在搜索栏上吗?

Is there any way to get the full address displayed on the search view using the placeautocompletefragment from the Google Places API? It only displays the very first part of the whole address. I am sure that everything is implemented correctly and the full address is returned by place.getAddress(). Any idea why the full address is not displayed on the search bar?

autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
    ((EditText)autocompleteFragment.getActivity().findViewById(R.id.place_autocomplete_search_input)).setTextSize(18.0f);
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            ((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_prediction_primary_text))
            //autocompleteFragment.getActivity().findViewById(R.id.place_autocomplete_search_input))
                    .setText(place.getAddress());
            Log.i(TAG, "Place Selected: " + place.getName() + place.getAddress());
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });
    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .build();
    autocompleteFragment.setFilter(typeFilter);

我的应用程序的功能与该链接中的应用程序完全相同. https://maps-apis.googleblog.com/2015/12/autocomplete-widget-and-updated-place.html )

My app is functioning exactly like the one in this link. https://maps-apis.googleblog.com/2015/12/autocomplete-widget-and-updated-place.html)

我想要的是在搜索栏中以灰色(完整地址)显示文本,而不是黑色文本(地点名称).有人知道解决方案吗?预先谢谢你!

What I want is to display the text in gray(the full address) on the search bar instead of the black text(the name of a place). Does anyone know the solution? Thank you in advance!

推荐答案

使用以下代码创建 CustomPlaceAutoCompleteFragment

public class CustomPlaceAutoCompleteFragment extends PlaceAutocompleteFragment {
  Place place;

  @Override
  public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
    this.setOnPlaceSelectedListener(new PlaceSelectionListener() {
      @Override public void onPlaceSelected(Place place) {
        CustomPlaceAutoCompleteFragment.this.place = place;
      }

      @Override public void onError(Status status) {

      }
    });
    return super.onCreateView(layoutInflater, viewGroup, bundle);
  }

  @Override public void onActivityResult(int i, int i1, Intent intent) {
    super.onActivityResult(i, i1, intent);
    ((AppCompatEditText) getView()
        .findViewById(R.id.place_autocomplete_search_input)).setText(place.getAddress());
  }
}

在您的xml中,

<fragment
      android:id="@+id/place_autocomplete_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:name="com.yourpackagenaem.CustomPlaceAutoCompleteFragment"
      />

无法在onPlaceSelected中设置完整地址的原因是 PlaceAutoCompleteFragment 将onActivityResult上的文本名称设置为默认名称.因此,我们需要重写它.这就是为什么我建议您创建一个CustomPlaceAutoCompleteFragment的原因.我已经测试了代码.它像一种魅力一样工作.

The reason why you can't set the full address in onPlaceSelected is that PlaceAutoCompleteFragment set the name of text at onActivityResult as default. So, we need to override it. That's why I recommend you to create a CustomPlaceAutoCompleteFragment. I have tested the code. It worked like a charm.

希望它会有所帮助.快乐的编码:)

Hope it helps. Happy coding :)

这篇关于placeautocompletefragment完整地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆