gpt4 book ai didi

android - 如何在 Android 应用程序中嵌入 PlaceAutocompleteFragment

转载 作者:可可西里 更新时间:2023-11-01 11:42:45 25 4
gpt4 key购买 nike

我想在我的 Android 应用程序中实现 placeautocompletefragment。我正在尝试使用 this url 中的说明但它显示:

can not resolve symbol for placeautocompletefragment

Example of what I want to show

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)

getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

最佳答案

只需按照以下步骤操作,但在此之前请确保您已添加谷歌库和 API key

1.点击按钮添加波纹管

private void openAutocompleteActivity() {
try {
// The autocomplete activity requires Google Play Services to be available. The intent
// builder checks this and throws an exception if it is not the case.
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(mActivity);
startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
} catch (GooglePlayServicesRepairableException e) {
// Indicates that Google Play Services is either not installed or not up to date. Prompt
// the user to correct the issue.
GoogleApiAvailability.getInstance().getErrorDialog(mActivity, e.getConnectionStatusCode(),
0 /* requestCode */).show();
} catch (GooglePlayServicesNotAvailableException e) {
// Indicates that Google Play Services is not available and the problem is not easily
// resolvable.
String message = "Google Play Services is not available: " +
GoogleApiAvailability.getInstance().getErrorString(e.errorCode);

Log.e("Tag", message);
Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
}
}

2.然后添加这个函数

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE_AUTOCOMPLETE) {
if (resultCode == RESULT_OK) {
// Get the user's selected place from the Intent.
Place place = PlaceAutocomplete.getPlace(mActivity, data);
Log.i("TAG", "Place Selected: " + place.getName());

LatLng latLng = place.getLatLng();
latitude = latLng.latitude;
longitude = latLng.longitude;

Log.v(ApplicationsConstants.LoginDetails.LATITUDE, "" + latitude);
Log.v(ApplicationsConstants.LoginDetails.LONGITUDE, "" + longitude);

tvCity.setText(place.getName());
Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.CITY, "" + place.getName());
Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.LATITUDE, "" + latitude);
Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.LONGITUDE, "" + longitude);

} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(mActivity, data);
Log.e("TAG", "Error: Status = " + status.toString());
} else if (resultCode == RESULT_CANCELED) {
// Indicates that the activity closed before a selection was made. For example if
// the user pressed the back button.
}
}
}

关于android - 如何在 Android 应用程序中嵌入 PlaceAutocompleteFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com