GPS.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const SerialPort = require('serialport')
  2. const Readline = require('@serialport/parser-readline')
  3. const port = new SerialPort('/dev/ttyUSB0', {
  4. baudRate: 115200
  5. })
  6. const parser = port.pipe(new Readline({ delimiter: '\r\n' }))
  7. parser.on('data', (data) => {
  8. console.log(data);
  9. // if (data.includes('$GNRMC')) {
  10. // const gps = str_To_Gps84(data)
  11. // const gdGPS = gps84_To_Gcj02(gps.lat, gps.lon)
  12. // console.log(gdGPS);
  13. // }
  14. })
  15. /**
  16. * 北斗坐标系度分秒计算
  17. * @param msg
  18. * @return
  19. */
  20. function str_To_Gps84 (msg) {
  21. let lat = 0;
  22. let lon = 0;
  23. let split = msg.split(",");
  24. if (split[4] == ("N") || split[4] == ("S")) {
  25. const i = split[3].indexOf(".");
  26. const latInt = split[3].substring(0, i - 2);
  27. const latDec = split[3].substring(i - 2);
  28. const i1 = split[5].indexOf(".");
  29. const lonInt = split[5].substring(0, i1 - 2);
  30. const lonDec = split[5].substring(i1 - 2);
  31. lat = (+latInt) + (latDec) / 60;
  32. lon = (+lonInt) + (lonDec) / 60;
  33. }
  34. return { lat, lon };
  35. }
  36. /** 高德采用GCJ编码 https://blog.csdn.net/weixin_45566249/article/details/118305913
  37. * 84 to 火星坐标系 (GCJ-02)
  38. * @param lat
  39. * @param lon
  40. */
  41. function gps84_To_Gcj02 (lat, lon) {
  42. const pi = 3.1415926535897932384626;
  43. const a = 6378245.0;
  44. const ee = 0.00669342162296594323;
  45. let dLat = transformLat(lon - 105.0, lat - 35.0);
  46. let dLon = transformLon(lon - 105.0, lat - 35.0);
  47. const radLat = lat / 180.0 * pi;
  48. let magic = Math.sin(radLat);
  49. magic = 1 - ee * magic * magic;
  50. const sqrtMagic = Math.sqrt(magic);
  51. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  52. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  53. const Lat = +lat + dLat;
  54. const Lon = +lon + dLon;
  55. return { Lat, Lon }
  56. }
  57. function transformLat (x, y) {
  58. const pi = 3.1415926535897932384626;
  59. let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
  60. + 0.2 * Math.sqrt(Math.abs(x));
  61. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  62. ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  63. ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  64. return ret;
  65. }
  66. function transformLon (x, y) {
  67. const pi = 3.1415926535897932384626;
  68. let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
  69. * Math.sqrt(Math.abs(x));
  70. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  71. ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  72. ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
  73. * pi)) * 2.0 / 3.0;
  74. return ret;
  75. }