gpt4 book ai didi

android - placeautocompletefragment 完整地址

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:50 28 4
gpt4 key购买 nike

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

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 )

我想要的是在搜索栏上显示灰色文本(完整地址)而不是黑色文本(地名)。有谁知道解决方案?提前致谢!

最佳答案

使用下面的代码创建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 的原因。我已经测试了代码。它就像一个魅力。

希望对您有所帮助。快乐编码:)

关于android - placeautocompletefragment 完整地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373630/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com