【发布时间】:2020-05-08 06:37:59
【问题描述】:
如何使用 Google 地点 API 在 EditText 中获得地点建议?
【问题讨论】:
-
你知道如何从 Google place api 获取地点列表吗?
标签: java android google-maps android-studio google-places-api
如何使用 Google 地点 API 在 EditText 中获得地点建议?
【问题讨论】:
标签: java android google-maps android-studio google-places-api
我建议你先通过谷歌的get started guide。然后在place autocomplete guide 之后将AutocompleteSupportFragment 添加到您的活动中。请看下面的代码:
// Initialize the SDK
Places.initialize(getApplicationContext(), "YOUR_API_KEY");
// Create a new Places client instance
PlacesClient placesClient = Places.createClient(this);
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
希望这可以帮助您入门!
【讨论】: