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

前端 未结 2 1165
谎友^
谎友^ 2020-12-10 00:35

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 or

相关标签:
2条回答
  • 2020-12-10 00:39

    Google Map has callbacks to do that like this one or this one.

    Just implement them in your code and as soon as they're fired, just make a reverse geocode the coordinates. You actually found the most complicated part (you understood that you need to reverse geocode).

    0 讨论(0)
  • 2020-12-10 00:50

    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

    0 讨论(0)
提交回复
热议问题