Yan*_* Bi 5 android google-places-api
有没有办法使用 Google Places API 中的 placeautocompletefragment 来获取显示在搜索视图上的完整地址?它仅显示整个地址的第一部分。我确信一切都已正确实现,并且完整的地址由 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);
Run Code Online (Sandbox Code Playgroud)
编辑: 我的应用程序的功能与此链接中的应用程序完全相同。 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());
}
}
Run Code Online (Sandbox Code Playgroud)
在你的xml中,
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.yourpackagenaem.CustomPlaceAutoCompleteFragment"
/>
Run Code Online (Sandbox Code Playgroud)
之所以不能在onPlaceSelected中设置完整的地址,是因为PlaceAutoCompleteFragment将onActivityResult中的文本名称设置为默认。所以,我们需要重写它。这就是为什么我建议您创建 CustomPlaceAutoCompleteFragment。我已经测试了代码。它就像一个魅力。
希望能帮助到你。快乐编码:)
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |