- 設定Google Map的相依性(gradle dependency):
- 設定權限及API Key:
- 在AndroidManifest.xml中設定權限,如下:
- 在<application>節點內設定API Key:
在module層級的build.gradle中,其dependencies中多加一行如下設定:
compile 'com.google.android.gms:play-services:+'
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="Your_API_Key"/>
- 首先是Layout, activity_main.xml :
- 其實這樣就可以了,在不打程式碼的情況下,足以在畫面上顯示一個地圖版面。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
沒有留言 :
張貼留言