我是将unity工程导出为android工程,将定位脚本和需要的jar包依赖等加入,如下
LocationCustomDemo
package com.baidu.map.demo.layers;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.map.R;
import com.baidu.map.UnityPlayerActivity;
import com.baidu.map.demo.PermissionUtils;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
/**
* 展示定位图层自定义样式
*/
public class LocationCustomDemo extends Activity {
private static final String TAG = "unity";
private LocationClient mLocClient;
private MapView mMapView;
private BaiduMap mBaiduMap;
// 是否首次定位
boolean isFirstLoc = true;
private BitmapDescriptor bitmapA;
private Marker mMarker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locationcustom);
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
bitmapA = BitmapDescriptorFactory.fromResource(R.drawable.icon_monster);
checkPermissions();
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(LocationCustomDemo.this,"点击Marker图标", Toast.LENGTH_SHORT).show();
// 点击maker之后跳转回unity界面
Intent intent = new Intent(LocationCustomDemo.this, UnityPlayerActivity.class);
startActivity(intent);
return true;
}
});
}
/**
* 获取权限
*/
private void checkPermissions() {
PermissionUtils.applicationPermissions(this, new PermissionUtils.PermissionListener() {
@Override
public void onSuccess(Context context) {
// 定位初始化
initLocation();
}
@Override
public void onFailed(Context context) {
if (AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.LOCATION)
&& AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.STORAGE)) {
AndPermission.with(context).runtime().setting().start();
}
Toast.makeText(context, "文件读取权限或相机权限被禁用,请前往权限管理设置文件读取或相机权限", Toast.LENGTH_SHORT);
finish();
}
}, Permission.Group.STORAGE, Permission.Group.LOCATION);
}
/**
* 定位初始化
*/
public void initLocation(){
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
MyLocationListenner myListener = new MyLocationListenner();
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
// 打开gps
option.setOpenGps(true);
// 设置坐标类型
option.setCoorType("bd09ll");
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
/**
* 添加marker
*
* @param latLng 经纬度
*/
public void addMarker(LatLng latLng){
if (latLng.latitude == 0.0 || latLng.longitude == 0.0){
return;
}
MarkerOptions markerOptionsA = new MarkerOptions().position(latLng).yOffset(30).icon(bitmapA).draggable(true);
Log.d(TAG, "设置maker");
mMarker = (Marker) mBaiduMap.addOverlay(markerOptionsA);
// Log.d(TAG, "maker坐标"+mMarker.getPosition());
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// MapView 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
if (isFirstLoc) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(update);
isFirstLoc = false;
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(latLng).zoom(20.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
addMarker(latLng);
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())// 设置定位数据的精度信息,单位:米
.direction(location.getDirection()) // 此处设置开发者获取到的方向信息,顺时针0-360
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.build();
// 设置定位数据, 只有先允许定位图层后设置数据才会生效
mBaiduMap.setMyLocationData(locData);
}
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时必须调用mMapView. onResume ()
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时必须调用mMapView. onPause ()
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
bitmapA.recycle();
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
// 在activity执行onDestroy时必须调用mMapView.onDestroy()
mMapView.onDestroy();
}
}
因为我是unity对接,所以在UnityPlayerActivity下加入
// 这个名字随便起的,自己根据unity方法改
public void ShowToast()
{
startActivity(new Intent(this, LocationCustomDemo.class));
}
DemoApplication
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
// 默认本地个性化地图初始化方法
SDKInitializer.initialize(this);
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}
因为我在LocationCustomDemo
中申请权限用了AndPermission,代码不放了,可以去这里看
activity_locationcustom
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/btn_selector"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/dkgray"
android:contentDescription="定位icon"
android:orientation="horizontal">
RadioGroup>
LinearLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" >
com.baidu.mapapi.map.MapView>
LinearLayout>
AndroidManifest
有些配置是unity自带的
注意application的android:name=".BaiduMap.DemoApplication"
,如果不加这个会报错
Error inflating class com.baidu.mapapi.map.MapView
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.baidu.map"
android:installLocation="preferExternal">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-feature android:glEsVersion="0x00020000" />
<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_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:name=".BaiduMap.DemoApplication"
android:theme="@style/UnityThemeSelector">
<activity
android:name=".UnityPlayerActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="fullSensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
<meta-data
android:name="android.notch_support"
android:value="true" />
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="你的KEY" />
activity>
<meta-data
android:name="unity.build-id"
android:value="ccbf816c-d1a9-495b-80e6-9ed8063ef964" />
<meta-data
android:name="unity.splash-mode"
android:value="0" />
<meta-data
android:name="unity.splash-enable"
android:value="True" />
<meta-data
android:name="notch.config"
android:value="portrait|landscape" />
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" />
<activity android:name=".demo.layers.LocationCustomDemo" />
application>
manifest>
不要忘了在build.gradle
下加入下面这个,否则会报错
No implementation found for int com.baidu.mapsdkplatform.comjni.tools.JNITools.initClass (java.lang.Object, int)
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
还有百度地图需要的库,可以自己去官网下地图需要的库即可,这里我就不放出来了
记 : 定位定到几内亚或者地球经纬度的另一端是因为鉴权错误,确认AK和应用包名是否匹配,然后看下鉴权信息把应用的sha1码改成鉴权信息上的sha1码即可