使用JS将GPRMC转换成WGS84/GCJ02



背景 Background

在物联网相关的 应用中,我们经常都要将使用BG96的设备的GPS定位数据定位到Google Map中。
我们要将其GPRMC的数据转换成WGS84或GCJ02
In IoT system, we often get the GPS data from the device using BG96
We have to convert the raw data to WGS84/GCJ02 to show the device location on GoogleMap.

代码 Code


GPRMC 转换成WGS84


// The GPRMC gps data: 2259.312015N,11355.933779E
// @return  22.988533583333332 113.93222965
  setLatLng(v) {
    let vArr = v.split(',');
    let lat, lng;
    for (let i = 0; i < vArr.length; i++) {
      if (i == 0) {
        if (vArr[i].substring(vArr[i].length - 1) == "N") {
          lat = vArr[i].substr(0, vArr[i].length - 1);
        } else {
          lat = "-" + vArr[i].substr(0, vArr[i].length - 1);//if in south, it is  minus
        }
      }
      if (i == 1) {
        if (vArr[i].substring(vArr[i].length - 1) == "E") {
          lng = vArr[i].substr(0, vArr[i].length - 1);
        } else {
          lng = "-" + vArr[i].substr(0, vArr[i].length - 1);//if in west, it is minus
        }
      }
    }
    // the above data should be 2259.312015 11355.933779

    return {
      lat: this.gprmc2Wgs84(lat),
	  lng: this.gprmc2Wgs84(lng)
	};
  }

  
  //2259.312015 to 22.988533583333332
  gprmc2Wgs84(v: string) {
    let pointIndex = v.indexOf('.');
    if (pointIndex == -1) return null;
    let h = v.substr(0, pointIndex - 2);
    if (h.indexOf('-') !== -1) {//if minus
      return Number(h) - Number(v.substring(pointIndex - 2)) / 60;
    } else {
      return Number(h) + Number(v.substring(pointIndex - 2)) / 60;
    }
  }

WGS84是国际通用的坐标系,但是在GoogleMap当中,在大陆的部分必须转成GCJ02才能定位正确。
The WGS84 can be use all over the world on Google Map except Mainland China.
It some reasons, we have to use the GCJ02 on Google Map in Mainland China.


WGS84转GCJ02

我们可以通过下面这个开源的项目将WGS84转为GCJ02:
We can use the below open-source project to convert WGS84 to GCJ02:
https://github.com/hiwanz/wgs2mars.js

以上。
谢谢。
Thanks.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值