Google Maps API v2初探

來源:互聯網
上載者:User

 因為最近需要寫涉及到Google Maps的應用,所以就大概找了一些資料,發現網上大部分教材範例都是老版本的Google Maps api v1,這一套api已經被廢棄,因為無法註冊v1的api key,在網上找到了一篇《Google Maps Android API V2使用及問題解決》的部落格作者是聖騎士Wind (在這裡不得不感謝作者),這篇部落格對於本人的協助很大。自己又重新讀了官方文檔,終於能在AVD上運行地圖DEMO。

     正式開篇,首先,必須說明的是,假如你是新手,那麼在網上找到的所以關於google maps api v1的資料已經不能再用,因為這套api已經被google廢棄,也不再提供api key的註冊,所以現在一般都是開發api v2或者v3,在這裡說明的是v2,v2也是需要註冊api key的,但註冊方法略有不同,待會會說明。

Step1 你需要安裝 Google Play services SDK 

         在SDK Manager的Extras項就可以找到Google Play Services的安裝。

Step2 你需要為程式申請API KEY.具體的方法請參考聖騎士Wind的博文《Google Maps Android API V2使用及問題解決》,地址是http://www.cnblogs.com/mengdd/archive/2013/01/01/2841390.html,裡面關於擷取API KEY的部分寫的非常詳細,但我想可能會有許多人會像我一樣非常的粗心,在開啟Services服務的時候,會看錯,請注意,在API CONSLE裡開啟服務的時候要選擇Google
Maps Android API v2, 而不是Google Maps API v2。

Step3 把API Key加入程式中

          對AndroidManifest.xml進行修改。

          添加如下代碼。

        

<meta-data    android:name="com.google.android.maps.v2.API_KEY"    android:value="your_api_key"/>

 

 把your_api_key換成你獲得的api key。

 然後需要進行許可權註冊。同樣在AndroidManifest.xml中添加代碼。

 

<permission          android:name="com.example.mapdemo.permission.MAPS_RECEIVE"          android:protectionLevel="signature"/><uses-permission    android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

 將com.example.mapdemo替換成自己的包路徑,根據官方文檔的說明,

android.permission.ACCESS_COARSE_LOCATION Allows the API to use WiFi or mobile cell data (or both) to determine
the device's location.

android.permission.ACCESS_FINE_LOCATION Allows the API to use the Global Positioning System (GPS) to determine the
device's location to within a very small area.

一個是用基站定位+網路的方式進行定位的許可權,一個是用GPS進行精確定位的許可權。

Step4 OpenGL ES V2的支援

因為這個版本是支援OpenGL ES V2的,所以必須在AndroidManifest.xml中加入如下代碼:

<uses-feature  android:glEsVersion="0x00020000"  android:required="true"/>

Step5 布局檔案及原始碼

布局檔案

<?xml version="1.0" encoding="utf-8"?><fragment xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/map"  android:layout_width="match_parent"  android:layout_height="match_parent"  class="com.google.android.gms.maps.SupportMapFragment"/>

 然後MainActivity.java的原始碼:

package com.example.googletest;import android.os.Bundle; import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity{ @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     } }

 Notice: 因為我自己的AVD是Android API 10手機也是Android 2.3.3的版本,所以需要Support Library來輔助(當然,Support Library也可以在SDK Manager安裝),根據官方文檔的說明,如若手機版本是低於API 12的,就需要Support Library。MainActivity繼承FragmentActivity, 如若是高於12的,直接繼承Activity即可。

需要注意的:

1、 需要添加Google Play Service類庫,具體方法參考聖騎士Wind的博文。重新貼一次地址:http://www.cnblogs.com/mengdd/archive/2013/01/01/2841390.html。

2、 聖騎士Wind的博文裡面提到關於AVD能否啟動並執行問題,我可以很明確的說,可以在模擬器中運行,官方文檔並沒有說模擬器無法運行,而且經過實測是可以啟動並執行,只是需要在模擬器中安裝幾個apk, 一個是Google Play Service的APK,一個是Google地圖,而聖騎士Wind的博文裡面提供的兩個包其實是有缺漏的,他少提供一個Google地圖的APK,所以一開始我按照他的提示搞一直程式崩潰。。。Orz。具體大家可以自行搜尋下載。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.