[JAVA] How to display Map using Google Map API (Android)

Overview

I had a chance to use it in the development of my own Android application, so make a note of it. It can be used when you want to embed Google Map as part of the application.

Advance preparation

You must be logged in with your Google account ★ How to create a Google account

Install Google Play services

Install Google Play services from AndroidStudio> Tool> SDK Manager スクリーンショット 2019-04-28 21.53.24.png

Associate Google Play services with your project

-Implement Google Play services in build.gradle

build.gradle


dependencies {
    ...
    implementation 'com.google.android.gms:play-services:+'
}

Add meta-data to Android Manifest

Add the following after </ activity> and between </ application>

AndroidManifest.xml


<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

Get Google Map API Key

Key generation

  1. Enable the Maps SDK for Android from the API Library (https://console.developers.google.com/apis/library?project=golfscoremanagement) スクリーンショット 2019-04-28 22.19.29.png

  2. Create a project from Dashboard Project name: any name Location: If it is a self-made application, "no organization" is OK スクリーンショット 2019-04-28 22.23.59.png

  3. After that, create credentials in the created project ・ Select the API key on this screen スクリーンショット 2019-04-28 22.28.02.png

  4. Limit keys ・ At this point, your API key is created スクリーンショット 2019-04-28 22.31.59.png

Name: any name Application Limits: Select Android App Package name: Enter the package name of the target application SHA-1: ★ I will explain later ★ スクリーンショット 2019-04-28 22.34.35.png

  1. Confirmation of SHA-1 Open a terminal on your Mac and run the following command

Terminal


keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

・ Execution result (example)

Terminal


SHA1: 0R:B9:4Z:33:22:33:AA:BB:CC:55:66:77:GG:88:77:12:34:AB:CD:PP

There is a SHA-1 fingerprint in the part that is displayed as "Zura". Select the "0R: B9: 4Z: 33: 22: 33: AA: BB: CC: 55: 66: 77: GG: 88: 77: 12: 34: AB: CD: PP" part Paste it on SHA-1 in step 4 and press the Finish button to save it.

スクリーンショット 2019-04-28 22.58.10.png

After saving, check the API Key. (I will use it later) スクリーンショット 2019-04-28 23.00.08.png

Add resources

Create and add google_map_api.xml to res> values

google_map_api.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">API Key</string>
</resources>

★ API Key is the one obtained in step 5 (the part written here)

Add Permission

Describe the following in <manifest> (permission of Parmission)

AndroidManifest.xm


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Make OpenGL ES available

Also add to Android Manifest (same location as Parmission)

AndroidManifest.xm


    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

Added meta-data

Add it below the meta-data added at the beginning

AndroidManifest.xm


        </activity>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key"/>
</application>

Implementation

Created on the assumption that it will be incorporated into an existing application.

MapActivity.java

MapActivity.java


import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

activity_map.xml

activity_map.xml


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

Execution result

Screenshot_20190428-233343.jpg

Recommended Posts

How to display Map using Google Map API (Android)
[Rails] How to display Google Map
[Rails] google maps api How to post and display including map information
How to analyze with Google Colaboratory using Kaggle API
[Rails] How to calculate latitude and longitude with high accuracy using Geocoding API and display it on Google Map
How to use Map in Android ViewPager
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory
How to use the Google Cloud Translation API
How to get article data using Qiita API
How to display videos inline in Google Colab
[Rails] How to get location information using Geolocation API
Push notifications from Python to Android using Google's API
How to use Google Colaboratory
[Rails] How to display multiple markers on Google Map and display a balloon when clicked
[For beginners] How to display maps and search boxes using the GoogleMap Javascript API
How to reset password via API using Django rest framework
How to hide your Google Maps API key from HTML
Display the address entered using Rails gem'geocoder' on Google Map
Try to determine food photos using Google Cloud Vision API
How to install python using anaconda
Display multiple markers on Google Map
[Python3] Google translate google translate without using api
[Memo] How to use Google MµG
How to use OpenPose's Python API
How to use bing search api
Let's display the map using Basemap
[Python] How to use Typetalk API
Introducing Google Map API with rails
How to get followers and followers from python using the Mastodon API
Try to display google map and geospatial information authority map with python
Regularly upload files to Google Drive using the Google Drive API in Python
[Ruby on Rails] Display and pinning of GoolgeMAP using Google API
[Map display] Display a map from the address registered by the user using the Google Maps JavaScript API and Geocoding API!
How to display the progress bar (tqdm)
How to suppress display error in matplotlib
[Rails] How to introduce Google Analytics [Easy]
[Memo] How to use BeautifulSoup4 (1) Display html
[Python] How to display random numbers (random module)
How to draw a graph using Matplotlib
Bulk posting to Qiita: Team using Qiita API
How to update Google Sheets from Python
How to set up SVM using Optuna
How to deal with OAuth2 error when using Google APIs from Python
How to use Google Test in C
How to install a package using a repository
How to set xg boost using Optuna
How to use Google Assistant on Windows 10
Speech transcription procedure using Google Cloud Speech API
How to display multiplication table in python
How to display emoji on Manjaro Linux
How to search Google Drive with Google Colaboratory
How to display python Japanese with lolipop
[Android] Display images on the web in the info Window of Google Map
How to get a sample report from a hash value using VirusTotal's API
How to download youtube videos using pytube3
Image collection using Google Custom Search API
How to use GCP's Cloud Vision API
How to make a Pelican site map
[Rails] How to detect radical images by analyzing images using Cloud Vision API
Creating Google Spreadsheet using Python / Google Data API
How to display Hello world in python