(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,那你的地图会没法显示.