Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any way to get city name for Google Place picker android

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.

like image 299
Asesha George Avatar asked Mar 09 '23 16:03

Asesha George


1 Answers

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

like image 159
Yyy Avatar answered Apr 06 '23 02:04

Yyy