GeoCodingApp
GeoCodingApp
xml
**MapsActivity.java
package com.example.geocodingapp;
import android.annotation.SuppressLint;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
import com.example.geocodingapp.databinding.ActivityMapsBinding;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
Geocoder oGC=new Geocoder(this);
LatLng latLng=null;
try {
List<Address> list= oGC.getFromLocationName("Pandharpur,Maharashtra,India",1);
Address oAdr=list.get(0);
Log.d("LATIDUDE_VALUE",""+oAdr.getLatitude());
Log.d("LOGITUDE_VALUE",""+oAdr.getLongitude());
latLng=new LatLng(oAdr.getLatitude(),oAdr.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(oAdr.getLocality()));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
} catch (IOException e) {
throw new RuntimeException(e);
}
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(true);
}
}
**AndroidManifest.xml
<activity
android:name=".MapsActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
**Output