我正在使用地点自动完成功能从用户中选择城市,其工作正常,
但现在我想要城市和州名..
我的代码..
正在初始化
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);
活动结果
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();
}
希望这会对您有所帮助。谢谢..
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)