gpt4 book ai didi

android - 如何在 Autocomplete Places _ Places API 中获取城市和州名称

转载 作者:行者123 更新时间:2023-11-29 14:34:35 25 4
gpt4 key购买 nike

我正在使用 Places Autocomplete 从用户中选择城市,它工作正常,但现在我想要城市和州名..我的代码..

正在初始化

  List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields)
.setTypeFilter(TypeFilter.CITIES)
.setCountry("IN")
.build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

onActivity 结果

if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Place place = Autocomplete.getPlaceFromIntent(data);
Place place = Autocomplete.getPlaceFromIntent(data);

edit_profile_city_editText.setText(place.getName());

} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
Status status = Autocomplete.getStatusFromIntent(data);
Log.i("Autocomplete error", status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
}
}

从搜索栏中选择城市时,它同时显示城市和州..例如:金奈印度泰米尔纳德邦请帮助如何获得州名......

最佳答案

像这样使用:-

和onActivityResult

 if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
LatLng latLng = place.getLatLng();
double MyLat = latLng.latitude;
double MyLong = latLng.longitude;
Geocoder geocoder = new Geocoder(EditProfileActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String stateName = addresses.get(0).getAdminArea();
String cityName = addresses.get(0).getLocality();
edit_profile_city_editText.setText(place.getName() + "," + stateName);
} catch (IOException e) {
e.printStackTrace();

}

希望这对您有所帮助。谢谢..

关于android - 如何在 Autocomplete Places _ Places API 中获取城市和州名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321208/

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