1. Right-click the first package in the java folder, select New -> Activity -> Gallery.
2. Select the Google Maps Activity template then click Next. Accept the default name MapsActivity and click Finish.
3. This Google Maps Activity template adds a fragment (not constraint layout) to activity_maps.xml layout file to display Google map, and adds the MapsActivity class to MapsActivity.java that extends FragmentActivity (not AppCompatActivity as seen in Empty Activity template) and implements OnMapReadyCallBack. There are 2 overridden methods onCreate and onMapReady in this class. The class header is as follows:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
4. Open AndroidManifest.xml, find the following
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />
5. Replace YOUR_API_KEY with the API key you copied above(in previous blog) from Google Cloud Platform website.
6. Next, add the following code to AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
7. Open activity_start.xml, add a button to open Google map with the following attributes:
id: buttonViewMap,
onClick: viewMap, and
text: View Google Map.
8. Open StartActivity.java and implement the viewMap click event listener as follows.
public void viewMap(View view) {
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
}
9. Run your project. You will see the Google map as follows