android google map 使用


整個流程就是安裝 Google Play Service 的過程。

第一步

到 Android SDK Manager,把Tools更新到最新版吧!

主要更新的有
Android SDK Tools
Android SDK Platform-tools
Android SDK Build-tools



PS. 如果這一步發生版本衝突無法安裝,請參考這一篇(),把衝突的版本移除。個人是在ADT上出現了問題,所以把舊ADT 2.0 移除。

移除後,從Help -> Install New Software,再次從  https://dl-ssl.google.com/android/eclipse/ 安裝即可。

第二步:
更新完,會在<android-sdk>/extras/google/底下長出一個 google_play_services,

如果沒有,請重開Eclipse -> Window -> Android SDK Manager 查看 Package 中的 extra 是否存在 google_play_services。如果存在,即可以安裝。

如果還是不存在,請將<android-sdk>/extras/google底下的資料都刪除,再一次重開Eclipse -> Window -> Android SDK Manager 此時extra就會出現google_play_services了。

PS. 我們可能忘了<android-sdk>的位置。沒關係,Eclipse -> Window -> Preferences -> Android 就會看到了。

第2步之1:
安裝完,在Eclipse 中 import -> Existing Android Code Into Workspace,位置在<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib,記得複制一份到workspace下,再從workspace下的google-play-services_lib import進來Eclipse。google-play-service_lib完成import如下


第2步之2:
在想要使用google services的project上點右鍵 -> Properties -> android -> add... 將google_play_service_lib加入,如下

再選Java Build Path,將Android Private Libraries打勾



第三步:
修改 AndroidMainfest.xml 檔,紅色字為必需加的入的程式碼。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.example.bannerexample" >
    <!-- Include required permissions for Google Mobile Ads to run  這是網路權限 -->  
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <!--This meta-data tag is required to use Google Play Services. 這是meta-data的標記  maps.v2.API_KEY 就是google map的標記-->
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--Include the AdActivity configChanges 這是ads的activity -->
    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
</manifest>



第四步:

我們要先到eclipse中找到sha-fingerprint,每一個eclipse都有一個,記下這一串sha-fingerprint

 然後到  Google APIs Console  



 我們點擊左方的API和驗證->憑證。Google常改版,只要看到右邊的畫面就對了。目前個畫面已經有一個建立好的。我們點擊建立新的金鑰,再建一個新的。

 選android金鑰

注意紅框的地方,這是一個範例。這個畫面就是要我們塡入eclipse中找到sha-fingerprint,然後加上分號,再加上eclipse中要用的套件名稱。也就是說,如果不同電腦上的eclipse中的sha-fingerprint不同,就要建立一個新的或是在憑證中新增。同理套件名稱不同也要建立一個新的或是在憑證中新增。
好了,出現上面這個圖就是好了。因為我們只是示範方法,所以應用程式那一行我們可以看到跟憑證建立時那個例子一樣。如果我們換電腦,eclipse中的sha-finerprint不同或是套件名不同,我們就點擊  編輯允許使用的android應用程式,就不用一直建立新的憑證。如下,我們新增一個com.abc的套件。
 更新後,紅框多了一個可使用的套件。就是這樣簡單。

 注意api金錀的地方,那一串碼就是要填在AndroidMainfest.xml中
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
xxxxxxxxxxxx就是api金錀。

第四步:
為了方便測試我們直接import android中就附的maps範例,很有參考價值。這個範例放在
你的路徑\Android\android-sdk\extras\google\google_play_services\samples\maps 

記得import後,要新增套件名到憑證中哦!
執行畫面如下,可以一個一個玩玩看。








留言

這個網誌中的熱門文章

python 找圖自動點擊

Python pyserial 抓取系統內的 COM PORT

VBA EXCEL 工作表變化 馬上執行 的作法 Worksheet_Change