有没有办法获取Google Place Picker Android的城市名称 [英] is there any way to get city name for Google Place picker android
问题描述
我正在尝试从Google地点选择器中获取城市名称.有什么方法可以使用地点选择器API获取城市名称.我知道我们不能简单地通过放置place.getCity();
从API获取它,这是我的代码.
I am trying to get city name from Google place picker. is there any way that we can get city name using place picker API. i know we can not simply get it from API by placing place.getCity();
Below is my code.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_PLACE_PICKER){
if(resultCode==RESULT_OK){
Place place = PlacePicker.getPlace(data,this);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final CharSequence phone = place.getPhoneNumber();
final String placeId = place.getId();
final LatLng latLng = place.getLatLng();
if(place.getLocale() != null) {
String aname = place.getLocale().toString();
areaname.setText(aname);
}
location.setText(address);
gname.setText(name);
latlon.setText(String.valueOf(latLng));
}
}
}
我不希望城市中有任何空值.我读了Geocoder
大部分将给出空值.
I don't want any null values in city. i read Geocoder
will give mostly null values.
推荐答案
尝试使用地址解析器
final Place place = PlacePicker.getPlace(this,data);
Geocoder geocoder = new Geocoder(this);
try
{
List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude,place.getLatLng().longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
//String country = addresses.get(0).getAddressLine(2);
} catch (IOException e)
{
e.printStackTrace();
}
此外,您可以尝试调用网络服务并解析json结果以获取城市信息. 只需在网址下方传递从地点选择器获得的经纬度即可. 在结果中Administrative_area_level_2代表城市
Also you can try to call webservice and parse json result to get city. Just pass the lat/ lng you get from placepicker in below url. In the result administrative_area_level_2 represents the city
http://maps.googleapis. com/maps/api/geocode/json?latlng = 23,72& sensor = true
这篇关于有没有办法获取Google Place Picker Android的城市名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!