【Android 入門開發實戰:口罩地圖】Google Map 範本學習(1)

【Android 入門開發實戰:口罩地圖】線上免費講義課程目錄

Google Map 一開始不熟悉的同學,常常會覺得有很多複雜步驟與手續要設定,到處碰壁還建立不起來,所以這一節課程,KT 建議,我們先使用 Android Studio 內建自帶的範本專案,來幫我們快速完成 Google Map 的基礎建置。

建立 Google Map 專案

建立一個全新的專案,選擇 Google Map Acticity 範本。

google_maps_api.xml

完成建立專案後,預設會開啟 「google_maps_api.xml」,我們可以透過檔案中的連結(檔案中的第七行連結),快速幫我們申請到 API 金鑰。

開啟網址後,會請你登入 Google 帳戶

登入帳戶後,需要選擇這次的應用程式要建立在哪一個專案中,我們選擇「建立專案」,然後點擊「繼續」。

點擊建立 API 金鑰

建立完成金鑰,複製紅框處的金鑰文字

回到 「google_maps_api.xml」,將金鑰貼到最後一行,最後面的位置 YOUR_KEY_HERE 欄位中

例如: 你的金鑰是,「A1234567890abcdefghijk」

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">A1234567890abcdefghijk</string>

完成之後,我們就可以執行此專案。

我們就可以看到地圖,圖釘指在雪梨(-34.0, 151.0)位置的畫面

正確輸出結果

錯誤輸出結果

若沒有輸入金鑰或沒有輸入正確的金鑰,將會出現如下的錯誤畫面

可以在 loacat 看到錯誤訊息

E: Authorization failure.  Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
E: In the Google Developer Console (https://console.developers.google.com)
    Ensure that the "Google Maps Android API v2" is enabled.
    Ensure that the following Android Key exists:
    	API Key: YOUR_KEY_HERE
    	Android Application (<cert_fingerprint>;<package_name>): C5:80:1C:03:3C:D9:8A:80:1C:03:3C:D9:8A:C5;com.thishkt.myapplication

參考資料

Maps SDK for Android - Get Started
https://developers.google.com/maps/documentation/android-sdk/start?hl=zh-tw