Question

i am looking to implement reverse geocoding in android but i am having some problems could someone give me some directions? here is my code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textLong = (TextView) findViewById(R.id.textLong);
        textLat = (TextView) findViewById(R.id.textLat);


        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener listener = new myLocationListner();
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);




    }

    public class myLocationListner implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            if (location != null){

                double gettextLong = location.getLongitude();
                double gettextLat = location.getLatitude();

                textLat.setText(Double.toString(gettextLat));
                textLong.setText(Double.toString(gettextLong));


            }
Was it helpful?

Solution

You need to create a Geocoder object and use the .getFromLocation method:

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

I.e.:

public class myLocationListner implements LocationListener{

    Geocoder mGeocoder;

    // default constructor
    myLocationListener() {
        // instantiate geocoder object:
        mGeocoder = new Geocoder(this, Locale.getDefault());
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null){

            double gettextLong = location.getLongitude();
            double gettextLat = location.getLatitude();

            textLat.setText(Double.toString(gettextLat));
            textLong.setText(Double.toString(gettextLong));

            List<Address> addresses = mGeocoder.getFromLocation(gettextLat, getTextLong, 1);

        }
   }

see: http://developer.android.com/reference/android/location/Geocoder.html

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