Cordova plug-in development 2: high-precision positioning satellite data analysis

Article directory

  • 1. Final effect preview
  • 2. Coordinate acquisition method
  • 3. Encapsulate the general method of obtaining coordinates in a public class
  • 4. Encapsulate the startGeoLocation method in plug-in js
  • 5. How to encapsulate the main interface of the plug-in

1. Final effect preview

2. Coordinate acquisition method

 let obj = Object.assign({}, this.mapConfig.mapLocationObj)
     obj.isKeepCallBack = false
 let res = await this.utilsTools.getXYLocationDataByDeviceType(obj)

The mapLocationObj value is a public parameter encapsulated by the configuration file.

public mapLocationObj = {
packageName: 'com.xx.xxx',
delayTime: 2000,
intervalTime: 1000,
rodHeight: '0.5',
isKeepCallBack: false,
paramKey: 'getKeepData'
}

The data returned in res is the obtained coordinate data

3. Encapsulate the general method of obtaining coordinates in a public class

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) is the method to obtain high-precision coordinate data

returnNmeaDataNew(obj) {
return new Promise((resolve, reject) => {
GeolocationManager.startGeoLocation(obj, res => {
resolve(res)
}, fail => {
resolve(fail)
})
});

}

4. The startGeoLocation method is encapsulated in plug-in js

startGeoLocation:function(options,onSuccess,onError){
exec(onSuccess, onError, "GeolocationManager", "startGeoLocation", [options]);
}

5. How to encapsulate the main interface of the plug-in

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;
    }

Single positioning method

/**
     * Call single positioning
     * @param callbackContext
     * @param message
     */
    public void singleLocaiton(CallbackContext callbackContext, JSONObject message) throws JSONException {
        try {
            singleLocaitonCC = callbackContext;
            this.getLocation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Get the coordinates in getLocation and continuously update the coordinate values

 @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);
                }

            }
        }
    }

Satellite data analysis

 @SuppressLint("DefaultLocale")
    public void processNmeaData(String nmea) {
        if (nmea.length() == 0)
            return;
        if (!checkNMEAData(nmea)) {
            // It may be the command return value of A318
            return;
        }
        if (!nmea.startsWith("$GPPWR,") & amp; & amp; !nmea.startsWith("$GNGST,")
                 & amp; & amp; !nmea.startsWith("$GPGST,") & amp; & amp; !nmea.startsWith("$GLGSV,")
                 & amp; & amp; !nmea.startsWith("$GNGSV,") & amp; & amp; !nmea.startsWith("$BDGSV,")
                 & amp; & amp; !nmea.startsWith("$GPZDA,") & amp; & amp; !nmea.startsWith("$GPGSA,")
                 & amp; & amp; !nmea.startsWith("$GNVTG,") & amp; & amp; !nmea.startsWith("$GPVTG,")
                 & amp; & amp; !nmea.startsWith("$GNGSA,") & amp; & amp; !nmea.startsWith("$GPNTR,")
                 & amp; & amp; !nmea.startsWith("$GNGGA,") & amp; & amp; !nmea.startsWith("$GPGGA,")
                 & amp; & amp; !nmea.startsWith("$GPRMC,") & amp; & amp; !nmea.startsWith("$GPGSV,")
                 & amp; & amp; !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")) & amp; & amp; iFieldNum >= 14) {
            if (sField[6].trim().length() > 0) {
                switch (Integer.parseInt(sField[6])) {
                    case 0:// invalid solution
                        gpsStatue = 0;
                        break;
                    case 1:// single point solution
                        gpsStatue = 1;
                        break;
                    case 9:
                    case 2:// differential analysis
                        gpsStatue = 2;
                        break;
                    case 3:
                    case 4:// fixed solution
                    case 8:
                        gpsStatue = 4;
                        break;
                    case 5:// floating point solution
                        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"))) & amp; & amp; 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);
        }
    }

Continuously update coordinate data

void updateView() {
        String s = "Solution status:";
        switch (gpsStatue) {
            case 0:
                s + = "Invalid solution";
                break;
            case 1:
                s + = "Single point solution";
                break;
            case 2:
                s + = "Differential decomposition";
                break;
            case 4:
                s + = "Fixed solution";
                break;
            case 5:
                s + = "Floating point solution";
                break;

        }
        gpsStatueStr = s;
        try {
            if (null != singleLocaitonCC) {
                PositionInfo();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Return the coordinate data obtained in the plug-in

/**
     * Implement single positioning delegation
     * @throws JSONException
     */
    @Override
    public void PositionInfo() throws JSONException {
        sendPositionInfo(singleLocaitonCC);
    }

    /**
     * Return positioning 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);
        }
    }