说明:南方测绘RTK设备厂家提供的SDK中封装了蓝牙搜索连接、Cross账号登录等功能,我们通过Cordova插件进一步封装以便于我们可以在js中调用,目前项中实现了普通手机坐标定位(高德)、高精度差分设备模块定位(卫星数据)、手机获取南方测绘RTK硬件设备定位(蓝牙连接)这几种方式,后续应该会对接更多的RTK厂家。
let dTValue = localStorage.getItem("deviceType") || "0"
if (parseInt(dTValue) > 1) {
this.isHighPrecision = true
let obj = Object.assign({}, this.mapConfig.mapLocationObj)
obj.isKeepCallBack = true
let res = await this.utilsTools.getXYLocationDataByDeviceType(obj)
if (res && res["code"] == "200") {
this.showHighData(res)
}
} else {
this.isHighPrecision = false
}
说明:isHighPrecision为true页面顶部展示经纬度及高程模块,其他模式不展示,模式的设定单独一个页面,不同的模式存值deviceType不同,并且要方便之后拓展对接不同的厂商,获取到坐标数据res后我们将数据传递到showHighData方法中在页面展示,在mapLocationObj中配置了插件封装的参数,示例如下
public mapLocationObj = {
packageName: 'com.xxx.xxx',
delayTime: 300,
intervalTime: 500,
rodHeight: '0.0',
isKeepCallBack: false,
paramKey: 'getKeepData'
}
目前插件定义了六个值,并且之后扩展的话直接加参数就行,delayTime为插件请求方法的延迟时间,intervalTime为请求插件的间隔时间,rodHeight可作为页面设置杆高数值,isKeepCallBack为true表示持续返回坐标数据,false表示只获取一次坐标数据,paramKey参数的值代表执行插件中不同的方法
async getXYLocationDataByDeviceType(obj) {
let res;
if (this.isAndroid()) {
if (localStorage.getItem('deviceType') == '2') {
res = await this.returnNmeaDataNew(obj)
} else if (localStorage.getItem('deviceType') == '3') {
res = await this.settingRTKLocation(obj)
} else {
res = await this.returnGaoDeData()
}
} else {
res = {
latitude: 0,
longitude: 0,
altitude: 0,
gpsStatue: 0,
code: 500,
}
}
return res
}
目前插件只开发Android版本,浏览器或者IOS返回默认值0,当deviceType为3我们调用了南方测绘RTK获取坐标方式
settingRTKLocation(obj) {
return new Promise((resolve, reject) => {
RTKlocationManager.settingRTKLocation(obj, res => {
resolve(res)
}, fail => {
reject(fail)
})
});
}
settingRTKLocation:function(options,onSuccess,onError){
exec(onSuccess, onError, "RTKlocationManager", "settingRTKLocation", [options]);
}
方法入口
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if("settingRTKLocation".equals(action)){
message = args.getJSONObject(0);
isKeepCallBack = message.getBoolean("isKeepCallBack")||false;
paramKey = message.getString("paramKey");
this.getLocation();
this.goPageByParam(callbackContext,paramKey);
return true;
}
return false;
}
根据参数不同调用不同的方法
private void goPageByParam(CallbackContext callbackContext, String key){
singleLocaitonCC = callbackContext;
switch (key) {
case "connected":
SdkServiceApi.startConnectedActivity();
break;
......
case "getKeepData":
updateView();
break;
}
}
更新坐标数据
void updateView() {
String s = "解状态:";
switch (gpsStatue) {
case 0:
s += "无效解";
break;
case 1:
s += "单点解";
break;
case 2:
s += "差分解";
break;
case 4:
s += "固定解";
break;
case 5:
s += "浮点解";
break;
}
gpsStatueStr = s;
try {
if (null != singleLocaitonCC) {
PositionInfo();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
实现单次定位委托
public void PositionInfo() throws JSONException {
sendPositionInfo(singleLocaitonCC);
}
返回定位数据
public void sendPositionInfo(CallbackContext c) throws JSONException {
if(0.0 == latitude){
try {
Thread.sleep(delayTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(!isKeepCallBack){
locationDestory();
}
JSONObject json = new JSONObject();
json.put("latitude",latitude);
json.put("longitude",longitude);
json.put("altitude",altitude);
json.put("hrms",hrms);
json.put("vrms",vrms);
json.put("rms",rms);
json.put("type","rtkGps");
json.put("gpsStatue",gpsStatue);
if (0.0 != latitude) {
json.put("code", "200");
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
pluginResult.setKeepCallback(isKeepCallBack);
c.sendPluginResult(pluginResult);
} else {
json.put("code", "500");
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, json);
pluginResult.setKeepCallback(isKeepCallBack);
c.sendPluginResult(pluginResult);
}
}