【发布时间】:2019-10-29 09:15:29
【问题描述】:
是否可以在此布局中使用 Google Places API:
我突出显示了我想在其中使用 API 的 EditText 字段。
我已经搜索了许多关于如何执行此操作的指南,但似乎所有指南都使用了已弃用的代码。我对 Fragments 也很陌生,但我想更多地了解这个主题。
【问题讨论】:
标签: android android-fragments android-edittext google-places-api
是否可以在此布局中使用 Google Places API:
我突出显示了我想在其中使用 API 的 EditText 字段。
我已经搜索了许多关于如何执行此操作的指南,但似乎所有指南都使用了已弃用的代码。我对 Fragments 也很陌生,但我想更多地了解这个主题。
【问题讨论】:
标签: android android-fragments android-edittext google-places-api
如果您使用 Web API,这是可能的,但需要大量工作。
不过,好像以前有人做过:https://github.com/mukeshsolanki/Google-Places-AutoComplete-EditText
或者,您可以使用Places SDK for Android,它提供AutoCompleteSupportFragment 和AutoCompleteActivity。
【讨论】:
在该edittext上设置onclicklistener并在函数下方的onclick方法调用中
private void searchPlace() {
if (!Places.isInitialized(){ Places.initialize(this, getApplication().getString(R.string.google_maps_key)); }
// Set the fields to specify which types of place data to
// return after the user has made a selection.
List<Place.Field> fields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.ADDRESS);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder( AutocompleteActivityMode.FULLSCREEN, fields) .build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE); }
并在onresult方法中得到选择的结果
Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
destinationAddress = place.getAddress();
lblDestinationAddress.setText(destinationAddress);
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(this, "Some went wrong. Search again", Toast.LENGTH_SHORT).show();
Log.i(TAG, status.getStatusMessage());
}
依赖
//search places implementation
'com.google.android.libraries.places:places:2.0.0'
【讨论】: