is there any way to get city name for Google Place

2019-04-10 11:17发布

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));

            }
        }
    }

I don't want any null values in city. i read Geocoder will give mostly null values.

1条回答
疯言疯语
2楼-- · 2019-04-10 11:29

Try using geocoder

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();
}

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

查看更多
登录 后发表回答