• GPS坐标、火星坐标、百度坐标之间的转换--提供java版本转换代码


    参考文章:https://www.jianshu.com/p/c39a2c72dc65?from=singlemessage

    1、国内几种常用坐标系说明

    (1)名词解释

    坐标系统:用于定位的系统,就跟二维笛卡尔坐标系统一样,一个点使用(x,y),就能确定该点在笛卡尔坐标系统中的唯一位置。这里讲的坐标系统,相对于笛卡尔坐标系统,要复杂许多,但作用却都是一样,主要用于定位,也就是精确地定位地表上的一点。
    地理坐标系统:WGS84就是一种地理坐标系统。地理坐标坐标是对地球进行简单几何建模,比如将地球看成一个球体或者类球体,然后再将地表上点投影到该球面上形成的坐标就是地理坐标系统。WGS84就是定义了如何将地球抽象成球体或者类球体的规则。或者简单地来说,WGS84就是一堆参数,用于建立球体或者类球体,来近似地球。
    投影坐标系统:由于地球是一个球状,所以一般将其某个区域投影在平面上,形成的坐标系称为投影坐标系。
    (2)几种坐标说明

    WG-S84地理坐标系统,GPS仪器记录的经纬度信息,Google Earth采用,Google Map中国范围外使用,高德地图中国范围外使用。
    GCJ-02投影坐标系统火星坐标系,中国国家测绘局制定的坐标系统,由WGS-84加密后的坐标。Google中国和搜搜地图,arcgis地图,高德地图
    BD-09:    投影坐标系统百度坐标,GCJ-02加密后的坐标系,只适用于百度地图

    (在国内是不允许直接用WGS84坐标系标注的,必须经过加密后才能用。必须至少使用GCJ-02坐标系,或者使用在GCJ-02加密后再进行加密的坐标系,如百度坐标系)

    其他:搜狗地图:搜狗坐标系,图吧:图吧坐标等,估计也是在GCJ02基础上加密而成的,这里暂不涉及

    (3)转换方法

    1.可以通过嗲用直接的算法来转换(本文主要介绍该方法)
    2.可以通过Web API来转换
     3.可以通过第三方SDK API来转换

    2、下面给出前面3种坐标转换java算法

    package org.jeecg.modules.wangge.utils;
    
    /**
    *
     */
    
    public class GPSConverterUtils {
    
        public static final String BAIDU_LBS_TYPE = "bd09ll";
        public static double pi = 3.1415926535897932384626;
        public static double a = 6378245.0;
        public static double ee = 0.00669342162296594323;
    
        /**
         * 84 to 火星坐标系 (GCJ-02)
         * @param lat
         * @param lon
         */
        public static GPS gps84_To_Gcj02(double lat, double lon) {
            if (outOfChina(lat, lon))
            {
                return null;
            }
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 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);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            return new GPS(mgLat, mgLon);
        }
    
        /**
         * * 火星坐标系 (GCJ-02) to 84
         * * @param lon * @param lat * @return
         */
        public static GPS gcj_To_Gps84(double lat, double lon) {
            GPS gps = transform(lat, lon);
            double lontitude = lon * 2 - gps.getLon();
            double latitude = lat * 2 - gps.getLat();
            return new GPS(latitude, lontitude);
        }
    
        /**
         * 将 GCJ-02 坐标转换成 BD-09 坐标
         *
         * @param gg_lat
         * @param gg_lon
         */
        public static GPS gcj02_To_Bd09(double gg_lat, double gg_lon) {
            double x = gg_lon, y = gg_lat;
            double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
            double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
            double bd_lon = z * Math.cos(theta) + 0.0065;
            double bd_lat = z * Math.sin(theta) + 0.006;
            return new GPS(bd_lat, bd_lon);
        }
    
        /**
         * * 将 BD-09 坐标转换成GCJ-02 坐标 *
         * * @param
         * bd_lat * @param bd_lon * @return
         */
        public static GPS bd09_To_Gcj02(double bd_lat, double bd_lon) {
            double x = bd_lon - 0.0065;
            double y = bd_lat - 0.006;
            double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
            double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
            double gg_lon = z * Math.cos(theta);
            double gg_lat = z * Math.sin(theta);
            return new GPS(gg_lat, gg_lon);
        }
    
        /**
         * (BD-09)-->84
         * @param bd_lat
         * @param bd_lon
         * @return
         */
        public static GPS bd09_To_Gps84(double bd_lat, double bd_lon) {
    
            GPS gcj02 = bd09_To_Gcj02(bd_lat, bd_lon);
            GPS map84 = gcj_To_Gps84(gcj02.getLat(),
                    gcj02.getLon());
            return map84;
    
        }
    
        /**
         * is or not outOfChina
         * @param lat
         * @param lon
         * @return
         */
        public static boolean outOfChina(double lat, double lon) {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        }
    
        public static GPS transform(double lat, double lon) {
            if (outOfChina(lat, lon)) {
                return new GPS(lat, lon);
            }
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 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);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            return new GPS(mgLat, mgLon);
        }
    
        public static double transformLat(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;
        }
    
    
        public static double transformLon(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;
        }
    
    }
    package org.jeecg.modules.wangge.utils;
    
    public class GPS {
    
        private double lat;
        private double lon;
    
        public GPS(double lat, double lon) {
            this.lat = lat;
            this.lon = lon;
        }
    
        public double getLat() {
            return lat;
        }
    
        public void setLat(double lat) {
            this.lat = lat;
        }
    
        public double getLon() {
            return lon;
        }
    
        public void setLon(double lon) {
            this.lon = lon;
        }
    
        public String toString() {
            return "lat:" + lat + "," + "lon:" + lon;
        }
    }

    3、代码验证和调试

    坐标系的转换和验证,一定要在实际平台下进行比对和验证。下面分别提供百度地图,高德地图,arcigs地图的实例。

    (1)百度地图在线验证

    http://api.map.baidu.com/lbsapi/getpoint/index.html

    这个是百度拾取坐标系统,该系统使用的是BD-09百度坐标,如果用gps坐标在这里定位是有错误的。

    记住:哪个平台的坐标拾取系统,无论定位还是拾取坐标,用的就是他自己的坐标系。绝对不是GPS坐标,切记

    (2)高德地图leaflet验证实例-用GCJ-02坐标

    <!DOCTYPE html>
    <html>
    <head>
        <title>Quick Start - Leaflet</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css">
        <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
         <style type="text/css">
        body {
            padding: 0;
            margin: 0;
        }
        html,
        body
        {
            height: 100%;
        }
        #mapid
        {
            height: 75%;
         }
        </style>
    </head>
    <body>
    
    
    <div id="mapid" style="float:left;100%;height: 100%;">
    
    </div>
    
    <script>
    
        var mymap = L.map('mapid').setView([37.10375917564946,114.64100007966069], 14);
    
        this.baseLayer=L.tileLayer("http://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}",{
            attribution: '&copy; 高德地图',
            maxZoom: 15,
            minZoom: 4,
            subdomains: "1234"
        }).addTo(mymap);
    
    
        L.circleMarker([37.10375917564946,114.64100007966069], {
            stroke: true,
            color: '#aaaaaa',
            weight: 1,
            opacity:1,
            fillColor: '#00E400',
            fillOpacity: 1,
            radius:10
        }).addTo(mymap).bindPopup("<b>Hello world!</b><br />I am a popup.");
    
    </script>
    </body>
    </html>

    (3)arcgis地图leaflet验证实例--用GCJ-02坐标

    <!DOCTYPE html>
    <html>
    <head>
        <title>Quick Start - Leaflet</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css">
        <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
         <style type="text/css">
        body {
            padding: 0;
            margin: 0;
        }
        html,
        body
        {
            height: 100%;
        }
        #mapid
        {
            height: 75%;
         }
        </style>
    </head>
    <body>
    
    
    <div id="mapid" style="float:left;100%;height: 100%;">
    
    </div>
    
    <script>
    
        var mymap = L.map('mapid').setView([37.10375917564946,114.64100007966069], 14);
    
        L.tileLayer('http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}', {
            maxZoom: 18,
            minZoom: 4,
            pane: 'overlayPane'
        }).addTo(mymap);
    
    
        L.circleMarker([37.10375917564946,114.64100007966069], {
            stroke: true,
            color: '#aaaaaa',
            weight: 1,
            opacity:1,
            fillColor: '#00E400',
            fillOpacity: 1,
            radius:10
        }).addTo(mymap).bindPopup("<b>Hello world!</b><br />I am a popup.");
    
    </script>
    </body>
    </html>

     最后附上一个在线工具:地址转经纬度,地址:https://maplocation.sjfkai.com/

  • 相关阅读:
    Learning Experience of Big Data:The First Day-Try to set up a network connection on my virtural machine
    Learning Experience of Big Data: Learn to install CentOs 6.5 on my laptop
    事物总线模式实例——EventBus实例详解
    软件架构——事件总线模式
    阅读《大型网站技术架构》,并结合"重大需求征集系统"有感
    淘宝网的六个质量属性
    读架构漫谈博文有感
    06软件需求读书笔记(六)
    .NET应用程序性能优化
    【转】消息队列设计精要
  • 原文地址:https://www.cnblogs.com/tiandi/p/13681670.html
Copyright © 2020-2023  润新知