4

Iam using Places Autocomplete for selecting city from user, its working fine, But now i want both city and state name.. my code..

Initialising

  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 Result

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) {
            }
        }

While selecting city from search bar its showing both city and state.. eg:Chennai TamilNadu, India Kindly help how to get state name also...

Navin Kumar
  • 3,393
  • 3
  • 21
  • 46

3 Answers3

7

Use like this:-

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

            }

Hope this will help you.Thanks..

Navin Kumar
  • 3,393
  • 3
  • 21
  • 46
Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
3

Try something like this:

 Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
 List<Address> addresses = new ArrayList<>();
 addresses = geocoder.getFromLocation(lat, lng, 1);
 String country = addresses.get(0).getCountryName();
Ervin
  • 336
  • 1
  • 10
2

I think these will work for you;

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);

or

String address = addresses.get(0).getSubLocality();
String cityName = addresses.get(0).getLocality();
String stateName = addresses.get(0).getAdminArea();
Selman Tosun
  • 428
  • 5
  • 14