1.最终效果预览
2.坐标获取方法
let obj = Object.assign({}, this.mapConfig.mapLocationObj)
obj.isKeepCallBack = false
let res = await this.utilsTools.getXYLocationDataByDeviceType(obj)
mapLocationObj值为配置文件封装的公共参数
public mapLocationObj = {
packageName: 'com.xx.xxx',
delayTime: 2000,
intervalTime: 1000,
rodHeight: '0.5',
isKeepCallBack: false,
paramKey: 'getKeepData'
}
res中返回的数据即为获取到的坐标数据
3.在公共类中封装获取坐标的通用方法
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
}
res = await this.returnNmeaDataNew(obj)即为高精度坐标数据获取方法
returnNmeaDataNew(obj) {
return new Promise((resolve, reject) => {
GeolocationManager.startGeoLocation(obj, res => {
resolve(res)
}, fail => {
resolve(fail)
})
});
}
4.插件js中封装startGeoLocation方法
startGeoLocation:function(options,onSuccess,onError){
exec(onSuccess, onError, "GeolocationManager", "startGeoLocation", [options]);
}
5.插件主界面封装的方法
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if ("startGeoLocation".equals(action)) {
message = args.getJSONObject(0);
delayTime = message.getInt("delayTime");
intervalTime = message.getInt("intervalTime");
isKeepCallBack = message.getBoolean("isKeepCallBack")||false;
this.singleLocaiton(callbackContext, message);
return true;
}
return false;
}
单次定位方法
/**
* 调用单次定位
* @param callbackContext
* @param message
*/
public void singleLocaiton(CallbackContext callbackContext, JSONObject message) throws JSONException {
try {
singleLocaitonCC = callbackContext;
this.getLocation();
} catch (Exception e) {
e.printStackTrace();
}
}
getLocation中获取坐标并持续更新坐标值
@SuppressLint("MissingPermission")
public void getLocation() {
if (mLocationManager == null)
mLocationManager = (LocationManager) cordova.getContext().getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mLocationManager.addNmeaListener(mNmeaListener);
} else {
mNmeaListener2 = new OnNmeaMessageListener() {
@Override
public void onNmeaMessage(String arg1, long l) {
if (arg1 != null) {
processNmeaData(arg1);
updateView();
}else{
updateView();
}
}
};
if(mNmeaListener2!=null){
mLocationManager.addNmeaListener(mNmeaListener2);
}
}
}
}
卫星数据解析
@SuppressLint("DefaultLocale")
public void processNmeaData(String nmea) {
if (nmea.length() == 0)
return;
if (!checkNMEAData(nmea)) {
// 可能是A318的命令返回值
return;
}
if (!nmea.startsWith("$GPPWR,") && !nmea.startsWith("$GNGST,")
&& !nmea.startsWith("$GPGST,") && !nmea.startsWith("$GLGSV,")
&& !nmea.startsWith("$GNGSV,") && !nmea.startsWith("$BDGSV,")
&& !nmea.startsWith("$GPZDA,") && !nmea.startsWith("$GPGSA,")
&& !nmea.startsWith("$GNVTG,") && !nmea.startsWith("$GPVTG,")
&& !nmea.startsWith("$GNGSA,") && !nmea.startsWith("$GPNTR,")
&& !nmea.startsWith("$GNGGA,") && !nmea.startsWith("$GPGGA,")
&& !nmea.startsWith("$GPRMC,") && !nmea.startsWith("$GPGSV,")
&& !nmea.startsWith("$BDGSA,"))
return;
String[] sField = nmea.split(",");
int iFieldNum = stringNumbers(nmea, ",");
if (sField == null)
return;
if ((sField[0].equalsIgnoreCase("$GPGGA") || sField[0]
.equalsIgnoreCase("$GNGGA")) && iFieldNum >= 14) {
if (sField[6].trim().length() > 0) {
switch (Integer.parseInt(sField[6])) {
case 0:// 无效解
gpsStatue = 0;
break;
case 1:// 单点解
gpsStatue = 1;
break;
case 9:
case 2:// 差分解
gpsStatue = 2;
break;
case 3:
case 4:// 固定解
case 8:
gpsStatue = 4;
break;
case 5:// 浮点解
gpsStatue = 5;
break;
}
if (sField[2].trim().length() > 3) {
latitude = Double.parseDouble(sField[2].substring(0, 2))
+ Double.parseDouble(sField[2].substring(2)) / 60;
}
if (sField[3] == "S")
latitude *= -1.0;
if (sField[4].trim().length() > 4) {
longitude = Double.parseDouble(sField[4].substring(0, 3))
+ Double.parseDouble(sField[4].substring(3)) / 60;
}
if (sField[5] == "W") {
longitude *= -1.0;
longitude += 360;
}
if (sField[7].trim().length() > 0) {
m_SatNum = Integer.parseInt(sField[7]);
} else {
m_SatNum = 0;
}
if (sField[9].trim().length() > 0) {
if (sField[11].trim().length() == 0)
sField[11] = "0";
altitude = Double.parseDouble(sField[9]) + Double.parseDouble(sField[11]);
dUndulation = Double.parseDouble(sField[11]);
}
}
if (sField[13].trim().length() > 0) {
age = Double.parseDouble(sField[13]);
} else {
age = 99;
}
int m_Sec = 1, iSecOff = 0;
int m_Hour = 1, m_Min = 1;
if (sField[1].trim().length() >= 6) {
m_Hour = Integer.parseInt(sField[1].substring(0, 2));
m_Min = Integer.parseInt(sField[1].substring(2, 4));
m_Sec = Integer.parseInt(sField[1].substring(4, 6));
}
if (m_Sec > 59) {
iSecOff = m_Sec - 59;
m_Sec = 59;
}
if (m_Hour < 0 || m_Hour > 23 || m_Min < 0 || m_Min > 59
|| iSecOff > 60) {
return;
}
} else if (((sField[0].equalsIgnoreCase("$GPGST")) || (sField[0]
.equalsIgnoreCase("$GNGST"))) && iFieldNum >= 8) {
if (sField[7].trim().length() > 0) {
SigmaEast = Double.parseDouble(sField[7]);
}
if (sField[6].trim().length() > 0) {
SigmaNorth = Double.parseDouble(sField[6]);
}
if (sField[8].contains("*")) {
sField[8] = (sField[8].substring(0, sField[8].indexOf("*")));//
}
if (sField[8].trim().length() > 0) {
vrms = Double.parseDouble(sField[8]);
}
hrms = Math.sqrt(SigmaEast * SigmaEast + SigmaNorth * SigmaNorth);
rms = Math.sqrt(hrms * hrms + vrms * vrms);
}
}
持续更新坐标数据文章来源:https://www.toymoban.com/news/detail-739716.html
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();
}
}
将插件中获取的坐标数据返回文章来源地址https://www.toymoban.com/news/detail-739716.html
/**
* 实现单次定位 委托
* @throws JSONException
*/
@Override
public void PositionInfo() throws JSONException {
sendPositionInfo(singleLocaitonCC);
}
/**
* 返回定位 json
* @throws JSONException
*/
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","highGps");
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);
}
}
到了这里,关于Cordova插件开发二:高精度定位之卫星数据解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!