根据 Google 新政策“弃用通知:Google Play Services 版本的 Places SDK for Android”
注意:Google Play 服务版本的 Places SDK for Android(在 Google Play 服务 16.0.0 中)自 2019 年 1 月 29 日起弃用,并将于 2019 年 7 月 29 日关闭。新版本的Places SDK for Android 现已推出。我们建议尽快更新到新版本。有关详细信息,请参阅迁移指南。 Google Place Autocomplete
添加 Gradle 依赖项
implementation 'com.google.android.libraries.places:places:1.0.0'
初始化 Place API
Places.initialize(getApplicationContext(), "YOUR_API_KEY");
启动自动完成意图
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields)
.build(this);
startActivityForResult(intent, 1101);
onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_code) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "ManishPlace: " + place.getName() + ", " + place.getId());
txt_search.setText(place.getName());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
或关注我的 GitHub 教程
Google Place Search GitHub Repository