Files
tianrunCRM/Assets/3rd/LBSBaidu/Plugins/Android/BaiduLBS.java
2020-07-04 14:41:25 +08:00

181 lines
9.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tianrun;
import android.content.Intent;
import android.provider.Settings;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
//import com.baidu.mapapi.SDKInitializer;
import com.unity3d.player.UnityPlayer;
import org.json.JSONObject;
public class BaiduLBS {
public static LocationClient mLocClient;
static String listener;
/**
* 定位SDK监听函数
*/
public static class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (null != location && location.getLocType() != BDLocation.TypeServerError &&
location.getLocType() != BDLocation.TypeOffLineLocationFail &&
location.getLocType() != BDLocation.TypeCriteriaException) {
// MapView 销毁后不在处理新接收的位置
JSONObject jo = new JSONObject();
try {
jo.put("cmd", "onGetLocation");
if (location == null) {
jo.put("code", -1);
return;
}
// double mCurrentLat = location.getLatitude();
// double mCurrentLon = location.getLongitude();
// double mCurrentAccracy = location.getRadius();
jo.put("code", 0);
jo.put("latitude", String.format("%f", location.getLatitude()));
jo.put("longitude", String.format("%f", location.getLongitude()));
jo.put("AddrStr", location.getAddrStr());
jo.put("locationInfor", location.toString());
sendUnityMsg(jo.toString());
} catch (Exception e) {
System.out.println(e);
}
}
mLocClient.stop();
}
/**
* 回调定位诊断信息,开发者可以根据相关信息解决定位遇到的一些问题
*
* @param locType 当前定位类型
* @param diagnosticType 诊断类型1~9
* @param diagnosticMessage 具体的诊断信息释义
*/
@Override
public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) {
super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
StringBuffer sb = new StringBuffer(256);
sb.append("locType:" + locType);
sb.append("\n" + "诊断结果: ");
if (locType == BDLocation.TypeNetWorkLocation) {
if (diagnosticType == 1) {
sb.append("网络定位成功没有开启GPS建议打开GPS会更好" + "\n");
sb.append(diagnosticMessage);
} else if (diagnosticType == 2) {
sb.append("网络定位成功没有开启Wi-Fi建议打开Wi-Fi会更好" + "\n");
sb.append(diagnosticMessage);
}
} else if (locType == BDLocation.TypeOffLineLocationFail) {
if (diagnosticType == 3) {
sb.append("定位失败,请您检查您的网络状态" + "\n");
sb.append(diagnosticMessage);
}
} else if (locType == BDLocation.TypeCriteriaException) {
if (diagnosticType == 4) {
sb.append("定位失败,无法获取任何有效定位依据" + "\n");
sb.append(diagnosticMessage);
} else if (diagnosticType == 5) {
sb.append("定位失败无法获取有效定位依据请检查运营商网络或者Wi-Fi网络是否正常开启尝试重新请求定位" + "\n");
sb.append(diagnosticMessage);
} else if (diagnosticType == 6) {
sb.append("定位失败无法获取有效定位依据请尝试插入一张sim卡或打开Wi-Fi重试" + "\n");
sb.append(diagnosticMessage);
} else if (diagnosticType == 7) {
sb.append("定位失败,飞行模式下无法获取有效定位依据,请关闭飞行模式重试" + "\n");
sb.append(diagnosticMessage);
} else if (diagnosticType == 9) {
sb.append("定位失败,无法获取任何有效定位依据" + "\n");
sb.append(diagnosticMessage);
}
} else if (locType == BDLocation.TypeServerError) {
if (diagnosticType == 8) {
sb.append("定位失败请确认您定位的开关打开状态是否赋予APP定位权限" + "\n");
sb.append(diagnosticMessage);
}
}
System.out.println(sb.toString());
JSONObject jo = new JSONObject();
try {
jo.put("cmd", "onGetLocation");
jo.put("code", diagnosticType);
jo.put("msg", sb.toString());
sendUnityMsg(jo.toString());
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
public static void init(String _listener, String coorType) {
listener = _listener;
//在使用SDK各组件之前初始化context信息传入ApplicationContext
// SDKInitializer.initialize(UnityPlayer.currentActivity.getApplicationContext());
//自4.3.0起百度地图SDK所有接口均支持百度坐标和国测局坐标用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标默认是BD09LL坐标。
// SDKInitializer.setCoordType(CoordType.BD09LL);
// 初始定位
initLocation(coorType);
}
public static void initLocation(String coorType) {
mLocClient = new LocationClient(UnityPlayer.currentActivity.getApplicationContext());
//声明LocationClient类
mLocClient.registerLocationListener(new MyLocationListener());
//注册监听函数
// 定位初始化
LocationClientOption mOption = new LocationClientOption();
mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); // 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
mOption.setCoorType(coorType); // 可选默认gcj02设置返回的定位结果坐标系如果配合百度地图使用建议设置为bd09ll;
// mOption.setScanSpan(3000); // 可选默认0即仅定位一次设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
mOption.setIsNeedAddress(true); // 可选,设置是否需要地址信息,默认不需要
mOption.setIsNeedLocationDescribe(true); // 可选,设置是否需要地址描述
mOption.setNeedDeviceDirect(false); // 可选,设置是否需要设备方向结果
mOption.setLocationNotify(false); // 可选默认false设置是否当gps有效时按照1S1次频率输出GPS结果
mOption.setIgnoreKillProcess(true); // 可选默认true定位SDK内部是一个SERVICE并放到了独立进程设置是否在stop
mOption.setIsNeedLocationDescribe(true); // 可选默认false设置是否需要位置语义化结果可以在BDLocation
mOption.setIsNeedLocationPoiList(true); // 可选默认false设置是否需要POI结果可以在BDLocation
mOption.SetIgnoreCacheException(false); // 可选默认false设置是否收集CRASH信息默认收集
mOption.setOpenGps(true); // 可选默认false设置是否开启Gps定位
mOption.setIsNeedAltitude(false); // 可选默认false设置定位时是否需要海拔信息默认不需要除基础定位版本都可用
mLocClient.setLocOption(mOption);
}
// 取得我的当前位置
//可选设置返回经纬度坐标类型默认GCJ02
//GCJ02国测局坐标
//BD09ll百度经纬度坐标
//BD09百度墨卡托坐标
public static void getMyLocation(String CoorType) {
if (mLocClient != null) {
// if(mLocClient.isStarted()) return;
LocationClientOption option = mLocClient.getLocOption();
if (CoorType != null && CoorType != "") {
option.setCoorType(CoorType);
} else {
option.setCoorType("gcj02");
}
mLocClient.setLocOption(option);
mLocClient.start();
}
}
///打开gps设置
public static void guidSwitchGps() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//PermissionConstants.RC_GPS是请求码可以在onActivityResult中根据该请求码判断用户是否已经在设置页面打开位置服务
UnityPlayer.currentActivity.startActivity(intent);
}
public static void sendUnityMsg(String msg) {
UnityPlayer.UnitySendMessage(listener, "onCallback", msg);
}
}