Google Mapアプリを作る場合には、プロジェクトの新規作成でGoogle Map Activityを選択すると簡単に出来上がります。
![[Android & Kotlin] Google Map Activity で地図を表示 1x1.trans - [Android & Kotlin] Google Map Activity で地図を表示](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
2021.2.1
Google Map アプリ
マップアプリは、簡単にIntentでアプリに飛ばしてしまうこともできます
Intentで Google Map の表示
プロジェクトで Mapアプリ選択しても簡単にできます。
ここでは、デフォルトの地図を表示させた後に
特定の緯度、経度を指定して地図を拡大ズームさせるところまで試してみます。
Google Map プロジェクトの作成
プロジェクトを新規で作成するときにMAPアプリを選ぶと簡単にセットアップされます
Google Maps Activity を選択
![[Android & Kotlin] Google Map Activity で地図を表示 1x1.trans - [Android & Kotlin] Google Map Activity で地図を表示](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Projectが作成されると AndroidManifest.xml が現れてAPIキーを取得するための
URLが表示されます。
以前はリンクが google_maps_api.xml にありましたが、もうありません。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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キーを入手します
AndroidManifest.xml に説明がありましたが、取得したAPIキーを
local.propertiesに記述します
|
1 2 3 4 5 |
## This file is automatically generated by Android Studio. ... sdk.dir=C\:\\Users\\akira\\AppData\\Local\\Android\\Sdk MAPS_API_KEY=AIzaISyBC4N47_xxxxxxxxxxxxxxxxxxxHiRwU |
![[Android & Kotlin] Google Map Activity で地図を表示 1x1.trans - [Android & Kotlin] Google Map Activity で地図を表示](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
AndroidManifest.xml のYOUR_API_KEY を{MAPS_API_KEY}に置き換えます
|
1 2 3 |
<meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" /> |
これで設定が終わったので実行すると
シドニーにマークされた地図が表示されると思います。
![[Android & Kotlin] Google Map Activity で地図を表示 1x1.trans - [Android & Kotlin] Google Map Activity で地図を表示](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Map アプリコード
コードが以下のようにできています。
Google Maps Activity で作成されたままです
MapsActivity.kt
|
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 |
//package your.package.name import androidx.appcompat.app.AppCompatActivity 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.testgmapsimple.databinding.ActivityMapsBinding class MapsActivity : AppCompatActivity(), OnMapReadyCallback { private lateinit var mMap: GoogleMap private lateinit var binding: ActivityMapsBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMapsBinding.inflate(layoutInflater) setContentView(binding.root) // Obtain the SupportMapFragment and get notified when the map is ready to be used. val mapFragment = supportFragmentManager .findFragmentById(R.id.map) as SupportMapFragment 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 fun onMapReady(googleMap: GoogleMap) { mMap = googleMap // Add a marker in Sydney and move the camera val sydney = LatLng(-34.0, 151.0) mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney")) mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)) } } |
activity_maps.xml
|
1 2 3 4 5 6 7 8 9 |
<?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
|
1 2 3 4 5 |
## This file is automatically generated by Android Studio. ... sdk.dir=C\:\\Users\\akira\\AppData\\Local\\Android\\Sdk MAPS_API_KEY=AIzaISyBC4N47_xxxxxxxxxxxxxxxxxxxHiRwU |
緯度、経度、ズーム
できているコードに変更を加えてみます。緯度経度とズームの指定は
|
1 2 |
val cUpdate: CameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(35.68, 139.76), 12f) |
を使ってできます
MapsActivityの中のonMapReadyで設定します
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
... override fun onMapReady(googleMap: GoogleMap) { mMap = googleMap // Add a marker in Sydney and move the camera val sydney = LatLng(-34.0, 151.0) mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney")) mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)) val cUpdate: CameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(35.68, 139.76), 12f) mMap.moveCamera(cUpdate) } |
![[Android & Kotlin] Google Map Activity で地図を表示 1x1.trans - [Android & Kotlin] Google Map Activity で地図を表示](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
関連ページ:
- Google Map API キー取得
- Google Map Activity で地図を表示
- Google Map ズームとピンをアイコン画像に変更
- Google Map マーカーの追加
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