zoukankan      html  css  js  c++  java
  • 标准坐标系与火星坐标系(高德)百度坐标系之间互转

    这里先给大家介绍几个坐标系:

    1.WGS84:国际坐标系,为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。
    2.GCJ02:火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
    3.BD09:为百度坐标系,在GCJ02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标

    今天我要说的是,我们大部分的定位GPS设备以及硬件都是获取的WGS84坐标也就是我们说的标准坐标系

    但是我们软件应用层一般使用的高德,百度,腾讯的地图API,如果直接使用标准坐标系定位是会存在偏差的,所以就需要我们进行转换才能使用,这里给大家提供几种方式:

    1.硬件设备获取的GPS坐标格式是

    北纬:2937.1453(29°37.1453′) 东经:10629.7713(106°29.7713′)
    转换成度格式:
    北纬:29+37.1453/60=29.61908 东经:106+29.7713/60=106.49618
    

    就是需要我们转换后才能使用,在最近一个项目中,硬件设备传到服务器的GPS格式是【3028.0979】【10400.4032】但是服务器接收到是【30, 28, 9, 79】【1, 4, 0, 40, 32】这样的格式 有没有细心的朋友发现少了几位,0在byte字节传输中会被移除,所以这里就需要我们进行第一次转换:

    public static double bytetodouble(byte[] com) {
            String str = "";
            double dd = 0.00;
            for (int i = 0; i < com.length; i++) {
                String s = (int) com[i] + "";
                if (s.length() != 2 && i > 0) {
                    str += "0" + com[i] + "";
                } else {
                    str += com[i] + "";
                }
                dd = Integer.parseInt(str) / 10000.00;
            }
            return dd;
     }
    

    转后的结果为:【3028.0979,10400.4032】这才是硬件设备传给我们的数据

    其次我们还需要在做处理:

    double lat = 0.0;
    double lng = 0.0;
    String lats = ByteUtil.bytetodouble(_lat) + "";
    String lngs = ByteUtil.bytetodouble(_lng) + "";
    int index_lat = lats.indexOf(".") - 2;
    int index_lng = lngs.indexOf(".") - 2;
        lat = Double.parseDouble(lats.substring(0, index_lat)) + Double.parseDouble(lats.substring(index_lat)) / 60;
        lng = Double.parseDouble(lngs.substring(0, index_lng)) + Double.parseDouble(lngs.substring(index_lng)) / 60;
     
    

    这里转换后出来的结果是:【30.468298333333333,104.00672】当然当目前为止我们所转换处理的都任然是标准坐标系,还不是我们能放到高德之类的api去使用。

    最关键的就是转换成火星坐标:

    double[] gaodeGps = GpsUtil.toGCJ02Point(lat, lng, 7);// 进行纠偏
     
    

    这里我给大家提供一个封装的工具类:  

    import java.math.BigDecimal;
    import java.math.RoundingMode;
     
    /**
     * gps纠偏算法,适用于google,高德体系的地图
     */
    public abstract class GpsUtil {
     
        private final static double a = 6378245.0;
        private final static double pi = 3.1415926535897932384626;
        private final static double ee = 0.00669342162296594323;
     
        /**
         * 计算地球上任意两点(经纬度)距离
         * 
         * @param lat1
         *            第一点纬度
         * @param lng1
         *            第一点经度
         * @param lat2
         *            第二点纬度
         * @param lng2
         *            第二点经度
         * @return 返回距离 单位:米
         */
        public static double distance(double lat1, double lng1, double lat2, double lng2) {
            double a, b, R;
            R = 6378137; // 地球半径
            lat1 = lat1 * Math.PI / 180.0;
            lat2 = lat2 * Math.PI / 180.0;
            a = lat1 - lat2;
            b = (lng1 - lng2) * Math.PI / 180.0;
            double d;
            double sa2, sb2;
            sa2 = Math.sin(a / 2.0);
            sb2 = Math.sin(b / 2.0);
            d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
            return d;
        }
     
        /**
         * Description: WGS-84 to GCJ-02 <BR>
         * 
         * @author dsn
         * @date 2017年10月24日 下午2:09:27
         * @param latitude
         *            纬度
         * @param longitude
         *            经度
         * @return [纬度,经度]
         * @version 1.0
         */
        public static double[] toGCJ02Point(double latitude, double longitude) {
            double[] dev = calDev(latitude, longitude);
            double retLat = latitude + dev[0];
            double retLon = longitude + dev[1];
            return new double[] { retLat, retLon };
        }
     
        /**
         * Description: WGS-84 to GCJ-02 <BR>
         * 
         * @author dsn
         * @date 2017年10月24日 下午2:09:27
         * @param latitude
         *            纬度
         * @param longitude
         *            经度
         * @param scale
         *            经纬度保留小数位数
         * @return [纬度,经度]
         * @version 1.0
         */
        public static double[] toGCJ02Point(double latitude, double longitude, int scale) {
            double[] dev = calDev(latitude, longitude);
            double retLat = latitude + dev[0];
            double retLon = longitude + dev[1];
            return new double[] { new BigDecimal(retLat).setScale(scale, RoundingMode.DOWN).doubleValue(),
                    new BigDecimal(retLon).setScale(scale, RoundingMode.DOWN).doubleValue() };
        }
     
        /**
         * Description:GCJ-02 to WGS-84 <BR>
         * 
         * @author dsn
         * @date 2017年10月24日 下午2:09:54
         * @param latitude
         *            纬度
         * @param longitude
         *            经度
         * @return [纬度,经度]
         * @version 1.0
         */
        public static double[] toWGS84Point(double latitude, double longitude) {
            double[] dev = calDev(latitude, longitude);
            double retLat = latitude - dev[0];
            double retLon = longitude - dev[1];
            dev = calDev(retLat, retLon);
            retLat = latitude - dev[0];
            retLon = longitude - dev[1];
            return new double[] { retLat, retLon };
        }
     
        private static double[] calDev(double wgLat, double wgLon) {
            if (isOutOfChina(wgLat, wgLon)) {
                return new double[] { 0, 0 };
            }
            double dLat = calLat(wgLon - 105.0, wgLat - 35.0);
            double dLon = calLon(wgLon - 105.0, wgLat - 35.0);
            double radLat = wgLat / 180.0 * pi;
            double magic = Math.sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
            return new double[] { dLat, dLon };
        }
     
        private static boolean isOutOfChina(double lat, double lon) {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        }
     
        private static double calLat(double x, double y) {
            double 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;
        }
     
        private static double calLon(double x, double y) {
            double 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;
        }
     
    }
    

    最终转为后的GPS坐标为:【30.4656684,104.0090429】这里我是以标准坐标系转为火星坐标系的,其他的转法工具类里面有提供。

    还有一种方式,是高德API提供的其他坐标系转高德坐标系

    var gps = [116.3, 39.9];
    AMap.convertFrom(gps, 'gps', function (status, result) {
      if (result.info === 'ok') {
        var lnglats = result.locations; // Array.<LngLat>
      }
    });
    

    如有需要可以加我Q群【308742428】大家一起讨论技术。

    后面会不定时为大家更新文章,敬请期待。

    喜欢的朋友可以关注下。

     如果对你有帮助,请打赏一下!!! 

      

      

      

      

  • 相关阅读:
    CF1539 VP 记录
    CF1529 VP 记录
    CF875C National Property 题解
    CF1545 比赛记录
    CF 1550 比赛记录
    CF1539E Game with Cards 题解
    CF1202F You Are Given Some Letters... 题解
    vmware Linux虚拟机挂载共享文件夹
    利用SOLR搭建企业搜索平台 之九(solr的查询语法)
    利用SOLR搭建企业搜索平台 之四(MultiCore)
  • 原文地址:https://www.cnblogs.com/dsn727455218/p/9968813.html
Copyright © 2011-2022 走看看