问题
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);
EDIT: 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!
回答1:
Create CustomPlaceAutoCompleteFragment
with the code below
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());
}
}
In your xml,
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.yourpackagenaem.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 :)
回答2:
You don't need to create custom class for it just put it on Main class.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setText(mPlace.getAddress());
}catch (Exception e){
e.printStackTrace();
}
}
回答3:
Use AutocompleteFilter
to get full address in result like this :
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
.build();
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setFilter(typeFilter).build(activity);
startActivityForResult(intent, 1);
Src : https://developers.google.com/android/reference/com/google/android/gms/location/places/AutocompleteFilter
回答4:
Just call setText() on the PlaceAutocompleteFragment itself.
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
autocompleteFragment.setText(place.getAddress());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
}
来源:https://stackoverflow.com/questions/41373630/placeautocompletefragment-full-address