(6)Xamarin.android google map v2

原文 Xamarin.android google map v2android

Google Map v1已經在2013年的3月開始中止支持了,目前若要在你的Android手機上使用到Google Map,就必需要使用到Google Map v2的版本.在Xamarin要使用Google Map v2以前,必須要作的動做有下列幾項:
  1. 從你計算機裏的keystore裏面查詢我的的指紋憑證(SHA1)。
  2. 用SHA1指紋憑證到Google APIs網站申請key。
  3. 在Xamarin裏的Android SDK Manager去安裝Google Play services。
  4. 在Xamarin.Android項目中加入Google Play Services component。
    (原生的Android項目必須在這一步驟加入google-play-services_lib的Library。)
  5. 新建一個項目而且在AndroidManifest加入相關的權限設定。
  6. 在layout 畫面的Main.axml 中加入地圖控件

總結上述的步驟,在Android裏面要使用Map這樣的功能時,並不像IOS同樣,只要拖拉一個MapView到你的Layout View就能夠.所須要的步驟比較繁瑣.這些步驟又能夠被咱們分爲兩大部份.要使用Google Map在你的裝置上web

  1. 首先你要先上Google APIs網站上去開啓與Google Map相關的服務.
  2. 接着纔到Xamarin.Android項目中進行全部須要的設定修改.

接下來咱們就開始如何在Xamarin.Android項目裏使用Google map的相關設定:算法

1. 取得你的SHA1指紋憑證

1.1在Xamarin環境中,當咱們裝好Xamarin.android後,咱們要在底下的路徑去取得你的SHA1指紋憑證。
指紋憑證被放置在debug.keystore裏面,在Windows與OSX的存放路徑不同,請參考下列路徑:
Windows - C:\Users\[USERNAME]\AppData\Local\Xamarin\Mono for Android\debug.keystore OSX - /Users/[USERNAME]/.local/share/Xamarin/Mono for Android/debug.keystoreapi

1.2 開啓終端機窗口,你能夠直接輸入底下指令來取得你的SHA1指紋憑證. 在[USERNAME]的部分必需要改爲你的使用者名稱。
keytool -list -v -keystore /Users/[USERNAME]/.local/share/
Xamarin/Mono\ for\ Android/ 
debug.keystore -alias androiddebugkey -storepass android -keypass android
app

指令執行成功後能夠看到以下圖的信息,其中SHA1算法後面有一串16進位的數值。這就你我的的指紋憑證,這部分是咱們稍後在Google APIs網站上創建API Key會須要用到。eclipse

2. 創建你的Google API Project

2.1前往Google APIS網站去創建你的Google API( https://code.google.com/apis/console/ )ide

按下畫面中的Create Project 按鈕來創建一個Google API Project網站

2.2 創建好Google Project後,按下左邊的Service的標籤,接着在網頁中間會秀出全部的Service.
2.3 拖動下拉Bar,找到Google Map Android API v2 的Service,把這個Service的Switch控件打開成ON.請參考下圖:
到這邊,咱們就創建好Google project 與 Google Map Android API v2 服務.接下來咱們來看看要如何創建一個API鑰匙,這個鑰匙是當Xamarin.Anaroid要去取用Google Service API時要被用來驗證的Key.
3. 創建你的API鑰匙
接下來這個步驟咱們要創建Xamarin.Anaroid要去取用Google Service API時要被用來驗證的Key.
3.1 按下左邊API Access的標籤.
3.2 再接下來的畫面中點選Create new Android Key來創建相對應的鑰匙

3.3 在創建鑰匙的時候會須要剛剛在debug.keystore 裏面的SHA1算法指紋憑證.ui

3.4 從計算機中複製你的SHA1算法,貼到Configure Android Key for Xamarin Google Map API Android v2 窗口裏面。在你的SHA1指紋憑證後打上一個分號「;」接着輸入你應用程序的package name. 新增完成後按下肯定。
注:你的應用程序package name 是來自你在創建Android時,在AndroidManifest.xml檔案裏面所輸入的 package name.這部分兩邊的設定如果不同,或致使你的地圖沒法顯示.this

在上述程序代碼中,咱們透過Intent的GetStringExtra(「參數名稱」)來取得字符串型別的參數。事實上還能夠透過相似的方法取得不一樣型別的參數值。以下圖所示:
3.5 這時能夠看到被咱們新增出來的Android API Key.到目前爲止在Google APIs上的設定已經完成,接下來的就是要到Xamarin裏頭去創建使用Google map的Android App.
4. 創建使用Google Map 的Android Application
4.1 開啓Xamarin,創建一個Android Application方案
4.2 開啓Android SDK Manager 下載安裝Google play Service.

4.3 Binding Google Play Services 
安裝好Google Play Services後,接下來要在Xamarin.Android項目裏面Binding Java binding library. 這邊有兩個方式來綁定:

  1. 使用 Google Play Services component
  2. 手動bind the Google Play Services client library,這個方法比較相似在eclipse中開發使用Google Map的方式

Google Play Services component是Xamarin幫咱們簡化的Binding Java binding library所須要作的步驟,只要引用這個組件,就能夠很輕鬆的Binding Java binding library.因此在這部分咱們選擇使用Google Play Services component.

4.4 新增Google Play Services 組件 
展開你的Android Map項目,在Components文件夾按下鼠標右鍵,在彈出的窗口上點選 Get More Components…

4.5 搜尋並下載安裝Google Play Services.
5. 設定Android Application項目
5.1 修改AndroidManifest.xml檔案須要的權限 
開啓專案中的AndroidManifest.xml檔案,新增下方的Xaml檔案權限。
在  package="com.xamarin.docs.android.mapsandlocationdemo2"與 <permission android:name="<PACKAGE NAME>.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> 
這兩個地方要將package name修改爲你的Package name.
5.2 接着修改在< meta-data android:name="com.google.android.maps.v2.API_KEY"android:value="YOUR_API_KEY" />
標籤裏面Key值.這個Key是剛剛在Google APIs裏面創建的API Key。完整的修改請參照下方Xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="4.5"package="com.xamarin.docs.android.mapsandlocationdemo2" android:versionCode="6">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection 
state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to receive remote notifications from Google Play Services -->
<!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
<uses-permission android:name="<PACKAGE NAME>.permission.MAPS_RECEIVE" />
<permission android:name="<PACKAGE NAME>.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<!-- These are optional, but recommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission >android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:label="@string/app_name">
<!-- Put your Google Maps V2 API Key here. -->
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_API_KEY" />
</application>
</manifest>

5.3 新增一個MapFragment 地圖控件 ,新增完成會看到底下的畫面

<?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.MapFragment" />

5.4 編譯執行項目,能夠看到MapFragment畫面中地圖被下載回來. 
注:要看到地圖被加載,你的Android仿真器必須是要有支持Google Play service的版本。如果仿真器自己若是沒有 支持Google Play service,那你的地圖會沒法顯示.
 
相關文章
相關標籤/搜索