Google Maps API has provided awesome functionalities for android users, Like Search places, Pick address by tapping on map, Distance and time duration between two coordinates on the map.
In this blog, we are going to see how we can get the address of a location by dragging the map marker to that location.
So, let’s start with the first step. Declaring the dependencies related to maps.
1 2 |
implementation 'com.google.android.gms:play-services-maps:17.0.0' implementation 'com.google.android.gms:play-services-location:17.0.0' |
Now, let’s move to the layout changes. We would need to add a map fragment in the layout file according to the design needed.
1 2 3 4 5 6 |
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="400dp" tools:context=".MapsActivity" /> |
Then we need to declare the permissions related to the location and also need to check the permission at run time in devices above or equal to Android M.
1 2 3 4 5 6 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && mContext.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MAP_PIN_LOCATION_REQUEST_CODE); return; } } |
Now obtain SupportMapFragment and get notified when the map is ready by overriding OnMapReadyCallback
1 2 |
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); |
Now let’s override the onMapReady function
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).draggable(true).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); // Enable the zoom controls for the map mMap.getUiSettings().setZoomControlsEnabled(true); } |
In the next step, we will be adding a listener for the marker dragging. You can add this in onMapReady function. And with this, we can get the Address object when the marker dragging is ended by the user in onMarkerDragEnd method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
googleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() { @Override public void onMarkerDragStart(Marker marker) { } @Override public void onMarkerDrag(Marker marker) { } @Override public void onMarkerDragEnd(Marker marker) { LatLng latLng = marker.getPosition(); Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault()); try { android.location.Address address = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1).get(0); } catch (IOException e) { e.printStackTrace(); } } }); |
By following these steps you can get an address of a location by dragging the marker to that location.