Android百度定位SDK自v4.0版本之后开始引入了百度地图开放平台的统一AK验证体系。 通过AK机制,开发者可以更方便、更安全地配置自身使用的百度地图资源。考虑到大家的项目可能需要用到百度地图,下面就给大家介绍下接入百度地图SDK方法。
一、从Unity中导出安卓工程
导出工程时注意的问题:
(1)PlayerSetting中的bundle identifier要设置一下
(2)工程签名的制作
进入DOS界面,进去D:\ProgramFiles\Java\jdk1.6.0_10\bin>目录下,输入keytool -genkey -alias android.keystore -keyalg RSA -validity 100000 -keystore android.keystore参数意义:-validity主要是证书的有效期,写100000天;空格,退格键都算密码。命令执行后会在 D:\ProgramFiles\Java\jdk1.6.0_10\bin 目录下生成android.keystore文件。
(3)导出
二、将导出的项目利用eclipse接入百度地图SDK并生成jar包
我的eclipse工程目录:
资源的导入是参照百度地图SDK接入指南的:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/introduction
其中res/layout下的布局xml文件来自下载的BaiduLBS_AndroidSDK_Sample,具体路径是BaiduLBS_AndroidSDK_Sample\BaiduMap_AndroidSDK_v4.1.1_Sample\BaiduMapsApiDemo\res\layout
bin目录下生成了二手域名出售平台jar文件是原理:鼠标工程名字上右键Properties属性,勾选Is Library,之后在每次Project/Build All就可以实现更新;如下图所示:
下面是各个文件里面的代码
主入口文件MainActivity.java
package com.zhf; import android.content.Intent; import android.os.Bundle; import com.unity3d.player.UnityPlayerActivity; public class MainActivity extends UnityPlayerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public String getStringInJava() { return "i am a string from java"; } public void OpenNewScene() { Intent intent = new Intent(MainActivity.this, EmptyScene.class); this.startActivity(intent); } public void OpenBaiduMap() { Intent intent = new Intent(MainActivity.this, MyBaiduMapActivity.class); this.startActivity(intent); } }
EmptyScene.java
package com.zhf; import android.app.Activity; import android.os.Bundle; public class EmptyScene extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
MyBaiduMapActivity.java
package com.zhf; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.MapView; import android.app.Activity; import android.os.Bundle; public class MyBaiduMapActivity extends Activity { MapView mMapView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.bmapView); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } }
布局文件Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
主配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zhf" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" /> <application android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:icon="@drawable/app_icon" android:label="@string/app_name"> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="F7lsGhPU9a6XWiMIi6EbSTbsORx6zKr8" /> <activity android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.zhf.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> <activity android:name="com.zhf.EmptyScene" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> </activity> <activity android:name="com.zhf.MyBaiduMapActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> </activity> </application> <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" /> <uses-feature android:glEsVersion="0x00020000" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.touchscreen" android:required="false" /> <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" /> <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> </manifest>
BuildAll完毕
三、将导出的jar包应用到Unity中,实现SDK接入
需要导入到Unity中的文件(划了黑线的不要,以及armeabi-v7a中libmain.so、libmono.so和libunity.so文件不要):
在Unity中的视图结构:
必须在Plugins下面的Android目录下,注意assets是来自这里:
上面assets目录没有放到Unity工程中,程序运行了就崩溃,耗了一天时间找bug
四、编写C#脚本,实现交互
完成上面的之后新建一个场景,加入一个UGUI的Text控件,新建一个脚本Test
Test.cs代码:
using UnityEngine; using UnityEngine.UI; using System.Collections; public class Test : MonoBehaviour { public Text text; void Update () { if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home)) { Application.Quit(); } } void OnGUI() { if (GUILayout.Button("Open Map", GUILayout.Height(100))) { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("OpenBaiduMap"); } if (GUILayout.Button("Open New Scene", GUILayout.Height(100))) { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("OpenNewScene"); } if (GUILayout.Button("改变Label___New", GUILayout.Height(100))) { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); text.text = jo.Call<string>("getStringInJava"); } } }
脚本挂载在相机上,Text控件拖上去,然后开始打包
打包时注意:PlayerSetting中的bundleidentifier要填上对应的包名