真实GPS转腾讯/高德地图坐标系,百度地图与腾讯/高德地图坐标系转换

1.获取真实坐标GPS wgs84转为gcj编码方式

           positiontransform(array,manual){
            //定义常量
                var GPS = {
                    PI : 3.14159265358979324,
                    x_pi : 3.14159265358979324 * 3000.0 / 180.0,
                    //偏移计算
                    delta : function (lat, lon) {
                        var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
                        var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
                        var dLat = this.transformLat(lon - 105.0, lat - 35.0);
                        var dLon = this.transformLon(lon - 105.0, lat - 35.0);
                        var radLat = lat / 180.0 * this.PI;
                        var magic = Math.sin(radLat);
                        magic = 1 - ee * magic * magic;
                        var sqrtMagic = Math.sqrt(magic);
                        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
                        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
                        return {'lat': dLat, 'lon': dLon};
                    },
                    gcj_encrypt : function ( wgsLat , wgsLon ) {
                        if (this.outOfChina(wgsLat, wgsLon))
                            return {'lat': wgsLat, 'lon': wgsLon};

                        var d = this.delta(wgsLat, wgsLon);
                        return {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon};
                    },
                    outOfChina : function (lat, lon) {
                        if (lon < 72.004 || lon > 137.8347)
                            return true;
                        if (lat < 0.8293 || lat > 55.8271)
                            return true;
                        return false;
                    },
                    transformLat : function (x, y) {
                        var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
                        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
                        ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
                        ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
                        return ret;
                    },
                    transformLon : function (x, y) {
                        var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
                        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
                        ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
                        ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
                        return ret;
                    }
                };
            },
  1. gcj坐标数组转换成高德/腾讯地图坐标系数组,array为需要转换的坐标数组,lineArr为转换后的数组
                var lineArr = [];
                var lonMin = 1000,lonMax = 0,latMin = 1000,latMax = 0;
                //坐标转换完之后构造坐标数组
                for(let item of array){
                    var temp = GPS.gcj_encrypt(item.latitude, item.longitude);
                    // // 取小
                    lonMin = lonMin<temp.lon?lonMin:temp.lon;
                    lonMax = lonMax>temp.lon?lonMax:temp.lon;
                    latMin = latMin<temp.lat?latMin:temp.lat;
                    latMax = latMax>temp.lat?latMax:temp.lat;
					//将高德坐标系转成
                    lineArr.push([
                        temp.lon,
                        temp.lat
                    ])
                };

3.高德/腾讯坐标系转换成百度坐标系,输入高德/腾讯坐标经纬度,返回百度坐标系的经纬度

                function qqMapTransBMap(lng, lat) {
                    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
                    let x = lng;
                    let y = lat;
                    let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
                    let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
                    let lngs = z * Math.cos(theta) + 0.0065;
                    let lats = z * Math.sin(theta) + 0.006;
                    return {
                        lon: lngs,
                        lat: lats
                    }
                }

4.将百度地图经纬度转换为腾讯/高德地图经纬度,输入百度坐标系,返回高德/腾讯坐标系的经纬度

function bMapTransQQMap(lng, lat) {
      let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
      let x = lng - 0.0065;
      let y = lat - 0.006;
      let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
      let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
      let lngs = z * Math.cos(theta);
      let lats = z * Math.sin(theta);
      
      return {
          lng: lngs,
          lat: lats        
      }   
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值