Google Mapアプリを作る場合には、プロジェクトの新規作成でMAPアプリを選択すると簡単に出来上がります。
![[Android] Google Map 地図を表示させる 1x1.trans - [Android] Google Map 地図を表示させる](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Android Studio
2021.2.1
Google Map アプリ
マップアプリは、簡単にIntentでアプリに飛ばしてしまうこともできます
Intentでの簡単なGoogle Map 地図の表示
プロジェクトで Mapアプリ選択しても簡単にできます。
ここでは、デフォルトの地図を表示させた後に
特定の緯度、経度を指定して地図を拡大ズームさせるところまで試してみます。
Google Map プロジェクトの作成
プロジェクトを新規で作成するときにMAPアプリを選ぶと簡単にセットアップされます
Google Maps Activity を選択
![[Android] Google Map 地図を表示させる 1x1.trans - [Android] Google Map 地図を表示させる](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Projectが作成されると AndroidManifest.xml が現れてAPIキーを取得するための
URLが表示されます。
以前はリンクが google_maps_api.xml にありましたが、もうありません。
|
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <!-- TODO: Before you run your application, you need a Google Maps API key. To get one, follow the directions here: https://developers.google.com/maps/documentation/android-sdk/get-api-key Once you have your API key (it starts with "AIza"), define a new property in your project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the "YOUR_API_KEY" string in this file with "${MAPS_API_KEY}". --> |
以下の手順に沿ってAPIキーを入手します
Android アプリでGoogle Mapを使うためには、GoogleからAPIキーを取得する必要があります
APIキーを取得するや...
AndroidManifest.xml に説明がありましたが、取得したAPIキーを
local.propertiesに記述します
|
## This file is automatically generated by Android Studio. ... sdk.dir=C\:\\Users\\akira\\AppData\\Local\\Android\\Sdk MAPS_API_KEY=AIzaISyBC4N47_xxxxxxxxxxxxxxxxxxxHiRwU |
![[Android] Google Map 地図を表示させる 1x1.trans - [Android] Google Map 地図を表示させる](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
AndroidManifest.xml のYOUR_API_KEY を{MAPS_API_KEY}に置き換えます
|
<meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" /> |
これで設定が終わったので実行すると
シドニーにマークされた地図が表示されると思います。
![[Android] Google Map 地図を表示させる 1x1.trans - [Android] Google Map 地図を表示させる](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Map アプリコード
コードが以下のようにできています。
Google Maps Activity で作成されたままです
MapsActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
//package your.package.name; import androidx.fragment.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; import com.example.testgooglemap01.databinding.ActivityMapsBinding; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private ActivityMapsBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMapsBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney 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_maps.xml
|
<?xml version="1.0" encoding="utf-8"?> <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" /> |
ここはGoogle Map Keyを入れるだけです
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <application ... tools:targetApi="31"> <!-- TODO: Before you run your application, you need a Google Maps API key. To get one, follow the directions here: https://developers.google.com/maps/documentation/android-sdk/get-api-key Once you have your API key (it starts with "AIza"), define a new property in your project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the "YOUR_API_KEY" string in this file with "${MAPS_API_KEY}". --> <meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" /> <activity ... </activity> </application> </manifest> |
APIキーが追加されています
local.properties
|
## This file is automatically generated by Android Studio. ... sdk.dir=C\:\\Users\\akira\\AppData\\Local\\Android\\Sdk MAPS_API_KEY=AIzaISyBC4N47_xxxxxxxxxxxxxxxxxxxHiRwU |
緯度、経度、ズーム
できているコードに変更を加えてみます。緯度経度とズームの指定は
|
CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom( new LatLng(緯度, 経度), ズーム) |
を使ってできます
MapsActivityの中のonMapReadyで設定します
|
import com.google.android.gms.maps.CameraUpdate; ... @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney 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)); CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom( new LatLng(35.68, 139.76), 12); mMap.moveCamera(cUpdate); } |
![[Android] Google Map 地図を表示させる 1x1.trans - [Android] Google Map 地図を表示させる](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
関連ページ:
References:
API キーの取得 | Google Maps Android API | Google Developers
スタートガイド | Google Maps Android API | Google Developers
GitHub の Google マップ リポジトリ
Google Maps Android API – Google Developers
OnMapReadyCallback | Google APIs for Android | Google Developers
AndroidX の紹介 – Google Developers Japan
AndroidX の概要 | Android Developers