Add the Google Maps SDK
To begin, create a new Empty Android Application. Go to the app’s build gradle file and implement the Google Maps SDK.
Adding the Map to a Layout
To turn this fragment into a Google Maps fragment, set the name attribute to "com.google.android.gms.maps.SupportMapFragment"
Loading the Map
Now that we have added the map to the layout, we need to load it up. Go to the MainActivity file and import the SupportMapFragment class.
In the onCreate() method, get the reference to the Google Maps Fragment in the layout using the fragment manager.
Once we have the reference for the map, we need to attach a callback to it so we can know when the map is loaded onto the app.
In the class header, implement the callback and then hover over on top of it.
Select the “Implement Methods” option and implement the onMapRready() callback method
In the onCreate() method, grab the Google Maps fragment, call getMapAsync(), and pass in "this" for the callback.
The onMapReady() callback is triggered when the map is ready. We use this function to add contents to the map.
This is all we need to do to add the map to the app. The next thing we need to do is to generate an API key so we can use the Google Maps services.
Generating an API Key for Google Maps
On the left, scroll down until you see the Google Maps Platform tab and click on it.
In the content section, select the Maps SDK for Android option and enable the service.
Once the service is enabled, click on the Credentials tab on the left, then click on “CREATE CREDENTIALS” at the top and select API key.
Once the API key is created, copy it, and go back to Android Studio.
In the manifests file, add a meta-data element inside the application and set the key.
If we run the app, we should see a map.
If your map is empty, close the app and reinstall the app by uninstalling it first on the emulator.
That is all for this video. If you find this video helpful, please give it a like, share, and subscribe to support the channel.
©2019 - 2023 Codeible. All rights reserved.