坐标系转换工具

这一生的挚爱 提交于 2020-08-08 17:44:55

开发处理过一些地图类项目,对坐标系转换做一些工具类

WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,

谷歌地图采用的是WGS84地理坐标系(中国范围除外);

GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。

谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系

搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的

北斗芯片获取的经纬度为WGS84地理坐标
  • swift版

    class GPSTool {
    
        static let pi = 3.1415926535897932384626;
        static let a = 6378245.0
        static let ee = 0.00669342162296594323
        static let x_pi = pi * 3000.0 / 180.0
        
        static func gps84_To_Gcj02(lon:Double, lat:Double) ->CLLocationCoordinate2D?{
            if GPSTool.outOfChina(lon: lon, lat: lat) {
                return nil
            }
            var dLat = GPSTool.transformLat(x: lon - 105.0, y: lat - 35.0)
            var dLon = GPSTool.transformLon(x: lon - 105.0, y: lat - 35.0)
            let radLat = (lat / 180.0) * pi
            var magic = sin(radLat)
            magic = 1 - ee * magic * magic
            let sqrtMagic = sqrt(magic)
            dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi)
            dLon = (dLon * 180.0) / ((a / sqrtMagic) * cos(radLat) * pi)
            let mgLat = lat + dLat
            let mgLon = lon + dLon
            return  CLLocationCoordinate2D(latitude: mgLat, longitude: mgLon)
        }
        
        static func gcj02_To_Bd09(gg_lon:Double, gg_lat:Double)->CLLocationCoordinate2D {
            let x = gg_lon
            let y = gg_lat
            let z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
            let theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
            let bd_lon = z * cos(theta) + 0.0065;
            let bd_lat = z * sin(theta) + 0.006;
            return CLLocationCoordinate2D(latitude: bd_lat, longitude: bd_lon)
        }
        
        static func gps84_To_Bd09(lon:Double, lat:Double) ->CLLocationCoordinate2D?{
            if GPSTool.outOfChina(lon: lon, lat: lat) {
                return nil
            }
            if let gcl = GPSTool.gps84_To_Gcj02(lon: lon, lat: lat) {
                let bd = GPSTool.gcj02_To_Bd09(gg_lon: gcl.longitude, gg_lat: gcl.latitude)
                return bd
            }else{
                return nil
            }
        }
    
        static func gcj_To_Gps84(lon:Double, lat:Double) ->CLLocationCoordinate2D{
            let gps = GPSTool.transform(lon:lon, lat:lat)
            let longitude = lon * 2 - gps.longitude
            let latitude = lat * 2 - gps.latitude
            return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        }
        
        static func bd09_To_Gcj02(bd_lon:Double, bd_lat:Double) ->CLLocationCoordinate2D{
            let x = bd_lon - 0.0065
            let y = bd_lat - 0.006
            let z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi)
            let theta = atan2(y, x) - 0.000003 * cos(x * x_pi)
            let gg_lon = z * cos(theta)
            let gg_lat = z * sin(theta)
            return CLLocationCoordinate2D(latitude: gg_lat, longitude: gg_lon)
        }
        
        static func bd09_To_Gps84(bd_lon:Double, bd_lat:Double) ->CLLocationCoordinate2D{
            let gcj02 = GPSTool.bd09_To_Gcj02(bd_lon: bd_lon, bd_lat: bd_lat)
          let map84 = GPSTool.gcj_To_Gps84(lon: gcj02.longitude, lat: gcj02.latitude)
          return map84
        }
    
        static func transform(lon:Double, lat:Double) ->CLLocationCoordinate2D{
            if (GPSTool.outOfChina(lon: lon, lat: lat)) {
              return CLLocationCoordinate2D(latitude: lat, longitude: lon)
            }
            var dLat = GPSTool.transformLat(x: lon - 105.0, y: lat - 35.0)
            var dLon = GPSTool.transformLon(x: lon - 105.0, y: lat - 35.0);
            let radLat = (lat / 180.0) * pi
            var magic = sin(radLat)
            magic = 1 - ee * magic * magic
            let sqrtMagic = sqrt(magic)
            dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi)
            dLon = (dLon * 180.0) / ((a / sqrtMagic) * cos(radLat) * pi)
            let mgLat = lat + dLat
            let mgLon = lon + dLon
            return CLLocationCoordinate2D(latitude: mgLat, longitude: mgLon)
        }
        
        static func outOfChina(lon:Double, lat:Double) ->Bool{
            if (lon < 72.004 || lon > 137.8347) {
              return true
            }
            if (lat < 0.8293 || lat > 55.8271) {
              return true
            }
            return false
        }
        
        static func transformLat(x:Double, y:Double) ->Double{
            var ret =
              -100.0 +
              2.0 * x +
              3.0 * y +
              0.2 * y * y +
              0.1 * x * y +
              0.2 * sqrt(abs(x))
            ret +=
                ((20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0) /
              3.0
            ret +=
                ((20.0 * sin(y * pi) + 40.0 * sin((y / 3.0) * pi)) * 2.0) / 3.0;
            ret +=
                ((160.0 * sin((y / 12.0) * pi) + 320 * sin((y * pi) / 30.0)) *
                2.0) /
              3.0
            return ret
        }
        
        static func transformLon(x:Double, y:Double) ->Double{
            var ret =
              300.0 +
              x +
              2.0 * y +
              0.1 * x * x +
              0.1 * x * y +
              0.1 * sqrt(abs(x))
            ret +=
              ((20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0) /
              3.0
            ret +=
              ((20.0 * sin(x * pi) + 40.0 * sin((x / 3.0) * pi)) * 2.0) / 3.0;
            ret +=
              ((150.0 * sin((x / 12.0) * pi) + 300.0 * sin((x / 30.0) * pi)) *
                2.0) /
              3.0
            return ret
        }
    }
    
    
  • js版

gps对象

  • class Gps {
      constructor(longitude, latitude) {
        this.longitude = longitude;
        this.latitude = latitude;
      }
    
      setWgLongitude(longitude) {
        this.longitude = longitude;
      }
    
      setWgLatitude(latitude) {
        this.latitude = latitude;
      }
    
      getWgLongitude(longitude) {
        return this.longitude;
      }
    
      getWgLatitude(latitude) {
        return this.latitude;
      }
    
      toString() {
        return this.longitude + ',' + this.latitude;
      }
    
      toJson(count = 12) {
        let result = {
          longitude: this.longitude.toFixed(count) * 1,
          latitude: this.latitude.toFixed(count) * 1,
        };
        return result;
      }
    }
    module.exports = Gps;
    

工具

const pi = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
const x_pi = pi * 3000.0 / 180.0;

class GPSTool {

  static gps84_To_Gcj02(lon, lat) {
    if (GPSTool.outOfChina(lon, lat)) {
      return null;
    }
    let dLat = GPSTool.transformLat(lon - 105.0, lat - 35.0);
    let dLon = GPSTool.transformLon(lon - 105.0, lat - 35.0);
    let radLat = (lat / 180.0) * pi;
    let magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    let sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi);
    dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * pi);
    let mgLat = lat + dLat;
    let mgLon = lon + dLon;
    return new Gps(mgLon, mgLat);
  }

  static gps84_To_Bd09(lon, lat) {
    let gcl = GPSTool.gps84_To_Gcj02(lon, lat);
    let bd = GPSTool.gcj02_To_Bd09(gcl.getWgLongitude(), gcl.getWgLatitude());
    return bd;
  }

  static gcj_To_Gps84(lon, lat) {
    let gps = GPSTool.transform(lon, lat);
    let longitude = lon * 2 - gps.getWgLongitude();
    let latitude = lat * 2 - gps.getWgLatitude();
    return new Gps(longitude, latitude);
  }

  static gcj02_To_Bd09(gg_lon, gg_lat) {
    let x = gg_lon,
      y = gg_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 bd_lon = z * Math.cos(theta) + 0.0065;
    let bd_lat = z * Math.sin(theta) + 0.006;
    let gps = new Gps(bd_lon, bd_lat);
    return gps;
  }

  static bd09_To_Gcj02(bd_lon, bd_lat) {
    let x = bd_lon - 0.0065,
      y = bd_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 gg_lon = z * Math.cos(theta);
    let gg_lat = z * Math.sin(theta);
    return new Gps(gg_lon, gg_lat);
  }

  static bd09_To_Gps84(bd_lon, bd_lat) {
    let gcj02 = GPSTool.bd09_To_Gcj02(bd_lon, bd_lat);
    let map84 = GPSTool.gcj_To_Gps84(
      gcj02.getWgLongitude(),
      gcj02.getWgLatitude(),
    );
    return map84;
  }

  static outOfChina(lon, lat) {
    if (lon < 72.004 || lon > 137.8347) {
      return true;
    }
    if (lat < 0.8293 || lat > 55.8271) {
      return true;
    }
    return false;
  }

  static transform(lon, lat) {
    if (GPSTool.outOfChina(lon, lat)) {
      return new Gps(lon, lat);
    }
    let dLat = GPSTool.transformLat(lon - 105.0, lat - 35.0);
    let dLon = GPSTool.transformLon(lon - 105.0, lat - 35.0);
    let radLat = (lat / 180.0) * pi;
    let magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    let sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi);
    dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * pi);
    let mgLat = lat + dLat;
    let mgLon = lon + dLon;
    return new Gps(mgLon, mgLat);
  }

  static transformLat(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 * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0) /
      3.0;
    ret +=
      ((20.0 * Math.sin(y * pi) + 40.0 * Math.sin((y / 3.0) * pi)) * 2.0) / 3.0;
    ret +=
      ((160.0 * Math.sin((y / 12.0) * pi) + 320 * Math.sin((y * pi) / 30.0)) *
        2.0) /
      3.0;
    return ret;
  }

  static transformLon(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 * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0) /
      3.0;
    ret +=
      ((20.0 * Math.sin(x * pi) + 40.0 * Math.sin((x / 3.0) * pi)) * 2.0) / 3.0;
    ret +=
      ((150.0 * Math.sin((x / 12.0) * pi) + 300.0 * Math.sin((x / 30.0) * pi)) *
        2.0) /
      3.0;
    return ret;
  }
}

export default GPSTool;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!