Search code examples
androidgoogle-mapsgeocodinggoogle-geocoder

[Android][Google Maps]: Get the Address of location on touch


I have been googling this for hours but no luck so far.

I want to get the address of the location where the map is touched / tapped.

I understand that in order to get the address i need to reverse geocode the coordinates. But how do i get the coordinates from the map in the first place?


Solution

  • All you need to do is set up a OnMapClickListener, and then the onMapClick() override will give you a LatLng object. Then, use a Geocoder object to get the address of the point that was just clicked on.

    In this simple example, I've also added a Marker every time the user clicks a new point on the map.

    Here is the main piece of functionality that you need:

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    
            @Override
            public void onMapClick(LatLng point) {
                //save current location
                latLng = point;
    
                List<Address> addresses = new ArrayList<>();
                try {
                    addresses = geocoder.getFromLocation(point.latitude, point.longitude,1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                android.location.Address address = addresses.get(0);
    
                if (address != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                        sb.append(address.getAddressLine(i) + "\n");
                    }
                    Toast.makeText(MapsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
                }
    
                //remove previously placed Marker
                if (marker != null) {
                    marker.remove();
                }
    
                //place marker where user just clicked
                marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
    
            }
        });
    

    Here is the full class that I used to test this:

    public class MapsActivity extends AppCompatActivity {
    
        private GoogleMap mMap;
        private LatLng latLng;
        private Marker marker;
        Geocoder geocoder;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
    
            geocoder = new Geocoder(this, Locale.getDefault());
    
            setUpMapIfNeeded();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            setUpMapIfNeeded();
        }
    
        private void setUpMapIfNeeded() {
            // Do a null check to confirm that we have not already instantiated the map.
            if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                // Check if we were successful in obtaining the map.
                if (mMap != null) {
                    setUpMap();
                }
            }
        }
    
        private void setUpMap() {
    
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            mMap.getUiSettings().setMapToolbarEnabled(false);
    
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    
                @Override
                public void onMapClick(LatLng point) {
                    //save current location
                    latLng = point;
    
                    List<Address> addresses = new ArrayList<>();
                    try {
                        addresses = geocoder.getFromLocation(point.latitude, point.longitude,1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                    android.location.Address address = addresses.get(0);
    
                    if (address != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                            sb.append(address.getAddressLine(i) + "\n");
                        }
                        Toast.makeText(MapsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
                    }
    
                    //remove previously placed Marker
                    if (marker != null) {
                        marker.remove();
                    }
    
                    //place marker where user just clicked
                    marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
    
                }
            });
    
        }
    }
    

    Result of tapping the map in two different points:

    enter image description here

    enter image description here