In the Google Cloud console, create a project, enable Maps SDK for Android, and create an API key. Restrict it to Android apps with your package name and SHA-1 so nobody else can use it.
2. Create the project
In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24. Then add implementation("com.google.android.gms:play-services-maps:20.1.0") to app/build.gradle.kts and press Sync Now.
3. Keep the key out of your code
Put MAPS_API_KEY=your-key in local.properties, which Git ignores. app/build.gradle.kts reads it and fills the ${MAPS_API_KEY} placeholder in the manifest's com.google.android.geo.API_KEY meta-data. Your key never ends up on GitHub.
4. Add the map to the layout
Add a FragmentContainerView with android:name="com.google.android.gms.maps.SupportMapFragment". The Maps SDK draws the map inside it.
5. Wait for the map
Maps load in the background. Implement OnMapReadyCallback, call getMapAsync(this), and do everything in onMapReady.
6. Add markers
Create each place as a LatLng(latitude, longitude) and call addMarker. Give markers a title, a snippet and a colour with BitmapDescriptorFactory.defaultMarker.
7. Move the camera
LatLngBounds with newLatLngBounds fits all the markers on screen. animateCamera with newLatLngZoom flies smoothly to one place.
8. Change the map style
Switch between MAP_TYPE_NORMAL, MAP_TYPE_HYBRID and MAP_TYPE_TERRAIN with setMapType.
9. Run it
Run on a phone or an emulator with Google Play. Tap Fly to Pune and Map style.
If something goes wrong
A grey map with the Google logo: the key is missing, wrong, or the Maps SDK isn't enabled for it. Check Logcat for "Authorization failure".
The map never appears on the emulator: use a system image with Google Play.
Your key is on GitHub: delete it in the console, create a new one, and keep it in local.properties.
What's improved over the video
The API key stays out of your code and off GitHub, all markers fit on screen at launch, and there are buttons to fly to a city and change the map style.
Files affected
Check what to add and what to replace before copying code.
Waits for the map, adds three markers, fits them on screen, and handles the two buttons.
MainActivity.java
packagecom.example.googlemaps;importandroid.os.Bundle;importandroid.widget.Button;importandroidx.appcompat.app.AppCompatActivity;importcom.google.android.gms.maps.CameraUpdateFactory;importcom.google.android.gms.maps.GoogleMap;importcom.google.android.gms.maps.OnMapReadyCallback;importcom.google.android.gms.maps.SupportMapFragment;importcom.google.android.gms.maps.model.BitmapDescriptorFactory;importcom.google.android.gms.maps.model.LatLng;importcom.google.android.gms.maps.model.LatLngBounds;importcom.google.android.gms.maps.model.MarkerOptions;/**
* Shows a Google Map with a few markers. Tap a button to fly to a place or change the map style.
* The map loads asynchronously: everything happens in onMapReady.
*/publicclassMainActivityextendsAppCompatActivityimplementsOnMapReadyCallback{privatestaticfinalLatLngMUMBAI=newLatLng(19.0760,72.8777);privatestaticfinalLatLngPUNE=newLatLng(18.5204,73.8567);privatestaticfinalLatLngBENGALURU=newLatLng(12.9716,77.5946);privateGoogleMap map;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);Screens.setUp(this,R.layout.activity_main);SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);if(mapFragment !=null) mapFragment.getMapAsync(this);Button pune =findViewById(R.id.btnPune);Button style =findViewById(R.id.btnStyle);
pune.setOnClickListener(v ->{if(map !=null) map.animateCamera(CameraUpdateFactory.newLatLngZoom(PUNE,12));});
style.setOnClickListener(v ->{if(map ==null)return;// Cycle through the built-in map types.int next = map.getMapType()==GoogleMap.MAP_TYPE_NORMAL?GoogleMap.MAP_TYPE_HYBRID: map.getMapType()==GoogleMap.MAP_TYPE_HYBRID?GoogleMap.MAP_TYPE_TERRAIN:GoogleMap.MAP_TYPE_NORMAL;
map.setMapType(next);});}@OverridepublicvoidonMapReady(GoogleMap googleMap){
map = googleMap;
map.getUiSettings().setZoomControlsEnabled(true);
map.addMarker(newMarkerOptions().position(MUMBAI).title("Mumbai").snippet("The city of dreams"));
map.addMarker(newMarkerOptions().position(PUNE).title("Pune").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
map.addMarker(newMarkerOptions().position(BENGALURU).title("Bengaluru").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));// Start with all three markers in view.LatLngBounds bounds =newLatLngBounds.Builder().include(MUMBAI).include(PUNE).include(BENGALURU).build();findViewById(R.id.map).post(()-> map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,120)));}}