Question

I have a geocoder, gcd, and this line of code that reverse-geocodes

List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
                    if(addresses != null) {
                        Address returnedAddress = addresses.get(0);

                        for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                       strReturnedAddress = (returnedAddress.getAddressLine(i)).toString();

strReturnedAddress returns something like

10453 Central Bronx, New York City, NY

I only need the city name, which is New York City.

Removing parts of the string would be extremely hard since the output of the geocode can change. I just need the geocode to give me the city.

I have checked http://developer.android.com/reference/android/location/Geocoder.html but could not find an answer.

Was it helpful?

Solution

Awesome that you found a solution yourself.

You may although wish to use the approach below, which is more simple, since it utilises the Location API instead of having to iterate through the address list:

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

Even though this code is supplied by another user, I have implemented this exact approach myself and found it to be useful.

Hope this helps.

OTHER TIPS

I have found the solution myself and I am sharing it for future reference for others in need.

When the address is defined, all I need to do is address.getLocality() for the city name.

For this instance it would be

Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                       strReturnedAddress = (returnedAddress.getLocality().toString();
public class Locationfinder extends Activity implements LocationListener{   
     private TextView latituteField,longitudeField, Address;
      private LocationManager locationManager;
      private String provider;
    List<Address> mAddresses;
       double lat,lng;
       public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            latituteField = (TextView) findViewById(R.id.TextView02);
            longitudeField = (TextView) findViewById(R.id.TextView04);
            Address=(TextView)findViewById(R.id.TextView03);

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);

            // Initialize the location fields
            if (location != null) {

              onLocationChanged(location);
            } else {
              latituteField.setText("Location not available");
              longitudeField.setText("Location not available");
            }
       }

       protected void onResume() {
            super.onResume();
            locationManager.requestLocationUpdates(provider, 400, 1, this);
          }

//         Remove the locationlistener updates when Activity is paused 
          @Override
          protected void onPause() {
            super.onPause();
            locationManager.removeUpdates(this);
          }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        lat = (double) (location.getLatitude());
         lng = (double) (location.getLongitude());
        System.out.println("lat1: " + lat +"    " +"lng1" +lng);
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));

       Geocoder gcd = new Geocoder(getApplicationContext(),
               Locale.getDefault());
       try {


                 mAddresses = gcd.getFromLocation(lat,lng, 1);

           String address = mAddresses.get(0).getAddressLine(0);
              String city = mAddresses.get(0).getAddressLine(1);
              String country = mAddresses.get(0).getAddressLine(2);

              Address.setText("Address:- " + address + "city :" +city + "country : "+ country);


       } catch (IOException e) {
         e.printStackTrace();
         latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
       }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Disable GPS " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Enable GPS " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top