0% found this document useful (0 votes)
22 views5 pages

Display Map

Uploaded by

8daudio02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Display Map

Uploaded by

8daudio02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Display Map based on the current/given Location

Step 1: Create a new project


Step 2: goto Gradle Scripts > open build.gradle.kts and add this dependencies and sync
implementation("com.google.android.gms:play-services-maps:18.2.0")
implementation("com.google.android.gms:play-services-location:21.3.0")
implementation("com.karumi:dexter:6.2.1")
Step 3: Go to app > manifests > open AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value=""/>
Step 4 : get API KEY go to google cloud console.com > Create a account in google cloud
console > create project in google cloud console > API’s ^ Services > Enabled API’s
Services >

Step 5: Click on Enabled API’s and Services and Search for Maps SDK for Android
Step 6 : Enable it and API’s ^ Services > Credentials > create Credentials > API KEY will be
generated and copy that key and paste it in AndroidManifest.xml.
APIKEY sample : AIzaSyBRxjsSoKJdPOOJQLKgo_bVTJ3PXnx8Zyc

Write this code in activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
android:id="@+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Write code in MainActivity.java
public class MainActivity extends AppCompatActivity {

SupportMapFragment supportMapFragment;
FusedLocationProviderClient fusedLocationProviderClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.google_map);

fusedLocationProviderClient = (FusedLocationProviderClient)
LocationServices.getFusedOrientationProviderClient(this);

Dexter.withContext(getApplicationContext()).withPermission(Manifest.permission.ACCESS_F
INE_LOCATION)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse
permissionGrantedResponse) {
getCurrentLocation();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse
permissionDeniedResponse) {
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest
permissionRequest, PermissionToken permissionToken) {
}
});
}

private void getCurrentLocation() {


if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {

return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
if (location != null){
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions = new
MarkerOptions().position(latLng).title("Current Location");
googleMap.addMarker(markerOptions);

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));

} else {
Toast.makeText(MainActivity.this, "Please on your Location App Permission",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}

IT WILL SHOW USA address because google headquater is in USA so

You might also like