Google Maps
According to
Wikipedia Google maps provides satellite imagery, street maps, 360° panoramic views of streets, real-time traffic conditions and route planning. So if you want to add any feature related to maps in your app, you may use Google Maps API.
Prerequisites for Using Google Maps API
Google Developers Console account
How to get Google Maps API access key
Enable Google Maps SDK
- Login to Google developers console. Click on "New Project" and fill in the required information
- Click on "Create"
- From the side menu, go to APIs & Services -> Dashboard
- Click ENABLE APIS AND SERVICE
- Search and select Maps SDK for Android
- Click on Enable
After enabling Maps SDK, you now need API key credentials for integrating into your project.
How to get API key
- From side menu, go to APIs & Service -> Credentials
- Click on Create Credentials -> API key
A dialogue box containing your API key will now appear. Keep your API key 'very' private and do not share with anyone. You are now ready to create an app having Google Maps.
Integrating Maps in Android Studio Projects
- Start a new project in Android Studio.
- While adding activity, choose Google Maps Activity. This will add all important items needed for Maps integration in your app.
A file named google_maps_api.xml will be generated in values (debug) folder. You need to place your API key here.
- <resources>
- <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">PLACE_YOUR_KEY_HERE</string>
- </resources>
This key is then added in Android.manifest as meta data. You will find the below code in your manifest for this purpose (Note: If you are manually adding Google Maps Activity in your project, then you need to add this meta data yourself),
- <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" />
Run your app. You will see Google Maps Activity, with Maps opened in your application.
Code Explanation
Google Play Services (Maps Services) Plugin
For accessing Google Maps in your application, Google Play services plugin is needed in your app's build.gradle file. As you had added Google Maps Activity, Android Studio had added this plugin itself (Note: If you are manually adding Google Maps Activity in your project, then you need to add this dependency yourself)
- dependencies {...
- implementation 'com.google.android.gms:play-services-maps:16.0.0'...
- }
Permissions
For accessing location, ACCESS_FINE_LOCATION permission is needed in your manifest. You will find the below permission in your manifest file after adding Google Maps Activity in your project. (Note: If you are manually adding Google Maps Activity in your project, then you need to add this permission yourself),
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Layout
According to Android documentation, a Map Component fragment is the simplest way to add maps in an app. Fragment adds a wrapper around the map layout for handling the necessary life cycle events of a map. This component can simply be added in layout as follows,
- <fragment xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:map="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/map"
- android:name="com.google.android.gms.maps.SupportMapFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MapsActivity" />
As we had added Google Maps Activity provided by Android Studio by default, so this layout was automatically added in main activity's layout. (Note: If you are manually adding Google Maps Activity in your project, then you need to add this fragment yourself)
Activity
As you have added Maps Fragment in your layout, so you need to extend your Activity with FragmentActivity and implement OnMapReadyCallBack interface. For initialization, your map (with id map in layout) must be acquired using getMapAsync(OnMapReadyCallback). Below is the example Activity having map. (Note: This will be added automatically by Android Studio, when you add Google Maps Activity in your project)
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
- private GoogleMap mMap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {}
In onMapReady Function, you may add remaining required functionalities of Maps. All the available functions are available in Android's Map Fragment documentation.
Final Words
I am adding a sample project to my Blog post. Please update your API_KEY in the project. In case you have any query regarding maps, feel free to comment. Give a thumbs up if you find my blog helpful. Happy coding :)