原文 Xamarin.android google map v2android
總結上述的步驟,在Android裏面要使用Map這樣的功能時,並不像IOS同樣,只要拖拉一個MapView到你的Layout View就能夠.所須要的步驟比較繁瑣.這些步驟又能夠被咱們分爲兩大部份.要使用Google Map在你的裝置上web
接下來咱們就開始如何在Xamarin.Android項目裏使用Google map的相關設定:算法
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 androidapp
指令執行成功後能夠看到以下圖的信息,其中SHA1算法後面有一串16進位的數值。這就你我的的指紋憑證,這部分是咱們稍後在Google APIs網站上創建API Key會須要用到。eclipse
2.1前往Google APIS網站去創建你的Google API( https://code.google.com/apis/console/ )ide
按下畫面中的Create Project 按鈕來創建一個Google API Project網站
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
4.3 Binding Google Play Services
安裝好Google Play Services後,接下來要在Xamarin.Android項目裏面Binding Java binding library. 這邊有兩個方式來綁定:
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…
<?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>
<?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" />