1. 程式人生 > >各種經緯度座標系轉換-百度座標系、火星座標系、國際座標系

各種經緯度座標系轉換-百度座標系、火星座標系、國際座標系

(文章程式碼參考網上 測試沒什麼問題, 彙總整理希望對大家有幫助-dou )
WGS84:國際座標系,為一種大地座標系,也是目前廣泛使用的GPS全球衛星定位系統使用的座標系。
GCJ02:火星座標系,是由中國國家測繪局制訂的地理資訊系統的座標系統。由WGS84座標系經加密後的座標系。
BD09:為百度座標系,在GCJ02座標系基礎上再次加密。其中bd09ll表示百度經緯度座標,bd09mc表示百度墨卡托米制座標

一 : 百度座標與百度墨卡座標互轉

import java.util.HashMap;
import java.util.Map;
/** 
 * @ClassName: Baidu 
 * @Description: 百度座標與百度墨卡座標互轉
 * @author: Max dou
 * @date: 2017年4月11日 下午4:49:54 
 */
public class Baidu {
     public static void main(String[] args) {
            //百度座標轉百度墨卡座標
            Map<String,
Double>
location = convertMC2LL(13745329.000000, 5104310.500000); System.out.println(location.get("lng")+"==="+location.get("lat")); //百度墨卡座標轉百度座標 location = convertLL2MC(location.get("lng"),location.get("lat")); System.out.println(location.get("x")+"==="+location.get("y")); } private static Double EARTHRADIUS = 6370996.81; private static Double[] MCBAND = {12890594.86, 8362377.87, 5591021d, 3481989.83, 1678043.12, 0d}; private static Double[] LLBAND = {75d, 60d, 45d, 30d, 15d, 0d}; private static Double[][] MC2LL =
{{1.410526172116255e-8, 0.00000898305509648872, -1.9939833816331, 200.9824383106796, -187.2403703815547, 91.6087516669843, -23.38765649603339, 2.57121317296198, -0.03801003308653, 17337981.2}, {-7.435856389565537e-9, 0.000008983055097726239, -0.78625201886289, 96.32687599759846, -1.85204757529826, -59.36935905485877, 47.40033549296737, -
16.50741931063887, 2.28786674699375, 10260144.86}, {-3.030883460898826e-8, 0.00000898305509983578, 0.30071316287616, 59.74293618442277, 7.357984074871, -25.38371002664745, 13.45380521110908, -3.29883767235584, 0.32710905363475, 6856817.37}, {-1.981981304930552e-8, 0.000008983055099779535, 0.03278182852591, 40.31678527705744, 0.65659298677277, -4.44255534477492, 0.85341911805263, 0.12923347998204, -0.04625736007561, 4482777.06}, {3.09191371068437e-9, 0.000008983055096812155, 0.00006995724062, 23.10934304144901, -0.00023663490511, -0.6321817810242, -0.00663494467273, 0.03430082397953, -0.00466043876332, 2555164.4}, {2.890871144776878e-9, 0.000008983055095805407, -3.068298e-8, 7.47137025468032, -0.00000353937994, -0.02145144861037, -0.00001234426596, 0.00010322952773, -0.00000323890364, 826088.5}}
; private static Double[][] LL2MC = {{-0.0015702102444, 111320.7020616939, 1704480524535203d, -10338987376042340d, 26112667856603880d, -35149669176653700d, 26595700718403920d, -10725012454188240d, 1800819912950474d, 82.5}, {0.0008277824516172526, 111320.7020463578, 647795574.6671607, -4082003173.641316, 10774905663.51142, -15171875531.51559, 12053065338.62167, -5124939663.577472, 913311935.9512032, 67.5}, {0.00337398766765, 111320.7020202162, 4481351.045890365, -23393751.19931662, 79682215.47186455, -115964993.2797253, 97236711.15602145, -43661946.33752821, 8477230.501135234, 52.5}, {0.00220636496208, 111320.7020209128, 51751.86112841131, 3796837.749470245, 992013.7397791013, -1221952.21711287, 1340652.697009075, -620943.6990984312, 144416.9293806241, 37.5}, {-0.0003441963504368392, 111320.7020576856, 278.2353980772752, 2485758.690035394, 6070.750963243378, 54821.18345352118, 9540.606633304236, -2710.55326746645, 1405.483844121726, 22.5}, {-0.0003218135878613132, 111320.7020701615, 0.00369383431289, 823725.6402795718, 0.46104986909093, 2351.343141331292, 1.58060784298199, 8.77738589078284, 0.37238884252424, 7.45}}; /** * 墨卡託座標轉經緯度座標 * @param x * @param y * @return */ public static Map<String, Double> convertMC2LL(Double x, Double y) { Double[] cF = null; x = Math.abs(x); y = Math.abs(y); for (int cE = 0; cE < MCBAND.length; cE++) { if (y >= MCBAND[cE]) { cF = MC2LL[cE]; break; } } Map<String,Double> location = converter(x, y, cF); location.put("lng",location.get("x")); location.remove("x"); location.put("lat",location.get("y")); location.remove("y"); return location; } /** * 經緯度座標轉墨卡託座標 * @param lng * @param lat * @return */ private static Map<String, Double> convertLL2MC(Double lng, Double lat) { Double[] cE = null; lng = getLoop(lng, -180, 180); lat = getRange(lat, -74, 74); for (int i = 0; i < LLBAND.length; i++) { if (lat >= LLBAND[i]) { cE = LL2MC[i]; break; } } if (cE!=null) { for (int i = LLBAND.length - 1; i >= 0; i--) { if (lat <= -LLBAND[i]) { cE = LL2MC[i]; break; } } } return converter(lng,lat, cE); } private static Map<String, Double> converter(Double x, Double y, Double[] cE) { Double xTemp = cE[0] + cE[1] * Math.abs(x); Double cC = Math.abs(y) / cE[9]; Double yTemp = cE[2] + cE[3] * cC + cE[4] * cC * cC + cE[5] * cC * cC * cC + cE[6] * cC * cC * cC * cC + cE[7] * cC * cC * cC * cC * cC + cE[8] * cC * cC * cC * cC * cC * cC; xTemp *= (x < 0 ? -1 : 1); yTemp *= (y < 0 ? -1 : 1); Map<String, Double> location = new HashMap<String, Double>(); location.put("x", xTemp); location.put("y", yTemp); return location; } private static Double getLoop(Double lng, Integer min, Integer max) { while (lng > max) { lng -= max - min; } while (lng < min) { lng += max - min; } return lng; } private static Double getRange(Double lat, Integer min, Integer max) { if (min != null) { lat = Math.max(lat, min); } if (max != null) { lat = Math.min(lat, max); } return lat; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

二 : 國際座標即gps座標、百度座標、火星座標轉換

/** 
 * @ClassName: PositionUtil 
 * @Description: 
 * 各地圖API座標系統比較與轉換;
 * WGS84座標系:即地球座標系,國際上通用的座標系。裝置一般包含GPS晶片或者北斗晶片獲取的經緯度為WGS84地理座標系,
 * 谷歌地圖採用的是WGS84地理座標系(中國範圍除外);
 * GCJ02座標系:即火星座標系,是由中國國家測繪局制訂的地理資訊系統的座標系統。由WGS84座標系經加密後的座標系。
 * 谷歌中國地圖和搜搜中國地圖採用的是GCJ02地理座標系; BD09座標系:即百度座標系,GCJ02座標系經加密後的座標系;
 * 搜狗座標系、圖吧座標系等,估計也是在GCJ02基礎上加密而成的。 chenhua
 * @author: Max dou
 * @date: 2017年4月10日 下午4:17:44 
 */
public class PositionUtil {
    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;

    /**
     * 國際座標 to 火星座標系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
     * 
     * @param lat
     * @param lon
     * @return
     */
    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 國際座標 * * @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.getWgLon();
        double latitude = lat * 2 - gps.getWgLat();
        return new Gps(latitude, lontitude);
    }

    /**
     * 火星座標系 (GCJ-02) to 百度座標系 (BD-09) 的轉換演算法 將 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);
    }

    /**
     * * 火星座標系 (GCJ-02) to 百度座標系 (BD-09) 的轉換演算法 * * 將 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, 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) to 國際座標
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static Gps bd09_To_Gps84(double bd_lat, double bd_lon) {

        Gps gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);
        Gps map84 = PositionUtil.gcj_To_Gps84(gcj02.getWgLat(),
                gcj02.getWgLon());
        return map84;

    }

    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;
    }

    public static void main(String[] args) {
        Gps gps = new Gps(40.084481,116.395412 );
        System.out.println("gps :" + gps);
        Gps bd = bd09_To_Gcj02(gps.getWgLat(), gps.getWgLon());
        System.out.println("bd  :" + bd);
    }
}

/** 
 * @ClassName: Gps 
 * @Description: TODO
 * @author: Max dou
 * @date: 2017年4月10日 下午4:19:13 
 */
public class Gps {
    private double wgLat;
    private double wgLon;
    public Gps(double wgLat, double wgLon) {
    setWgLat(wgLat);
    setWgLon(wgLon);
    }
    public double getWgLat() {
    return wgLat;
    }
    public void setWgLat(double wgLat) {
    this.wgLat = wgLat;
    }
    public double getWgLon() {
    return wgLon;
    }
    public void setWgLon(double wgLon) {
    this.wgLon = wgLon;
    }
    @Override
    public String toString() {
    return wgLat + "," + wgLon;
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178

三 : 利用百度地圖api介面,座標轉換

 參考連結:http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition

此介面為免費介面,不過每天有限制

  
  • 1
  • 2
  • 3

四 : 利用百度地圖api介面,根據座標獲取地理位置資訊
參考連結:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
此介面為免費介面,不過每天有限制:
限制:

/**
 * @ClassName: BaiDuUtil
 * @Description: TODO
 * @author: Max dou
 * @date: 2017年4月10日 上午11:58:55
 */
public class BaiDuUtil {
    public static String getCity(String lat, String lng) {
        JSONObject objSrc = getLocationInfo(lat, lng);
        System.out.println(objSrc.toString());
        JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result")
                .getJSONObject("addressComponent");
        return obj.getString("city");
    }

    public static JSONObject getLocationInfo(String lat, String lng) {
        String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat
                + "," + lng + "&output=json&ak=" + "此處填寫百度開放平臺祕鑰"
                + "&pois=0";
        JSONObject obj = JSONObject.fromObject(HttpUtil.getRequest(url));
        return obj;
    }
    public static void main(String[] args) {
        System.out.println(BaiDuUtil.getCity("40.091637", "116.396764"));
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

五 : 阿里雲介面 : 根據座標獲取地理位置資訊
(此介面從網上找到的,已現在測試看沒有限制條件)

需要json解析包:
<dependency>      
    <groupId>net.sf.json-lib</groupId>     
    <artifactId>json-lib</artifactId>      
    <version>2.4</version>      
    <classifier>jdk15</classifier>    
</dependency>  

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/** 
 * @ClassName: GetLocation 
 * @Description: TODO
 * @author: Max dou
 * @date: 2017年4月10日 下午1:53:42 
 */
public class GetLocation {
     public static void main(String[] args) {  
             String add = getAdd("110.303959", "23.548133");  
             JSONObject jsonObject = JSONObject.fromObject(add);  
             JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));  
             JSONObject j_2 = JSONObject.fromObject(jsonArray.get(1));  
             JSONObject j_1 = JSONObject.fromObject(jsonArray.get(0));  
             String allAdd = j_2.getString("admName");  
             String arr[] = allAdd.split(",");  
             System.out.println("省:"+arr[0]+"\n市:"+arr[1]+"\n區:"+arr[2]+"\n路:"+j_1.getString("name")+"\n詳細地址:"+j_2.getString("name"));  
        }  

        public static String getAdd(String log, String lat ){  
            //lat 小  log  大  ,經過測試,資料的經緯度為國家座標即火星座標
            //引數解釋: 緯度,經度 type 001 (100代表道路,010代表POI,001代表門址,111可以同時顯示前三項)  
            String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=111";  
            String res = "";
            try {     
                URL url = new URL(urlString);    
                java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
                conn.setDoOutput(true);    
                conn.setRequestMethod("POST");    
                java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));    
                String line;    
               while ((line = in.readLine()) != null) {    
                   res += line+"\n";    
               }    
                in.close();    
            } catch (Exception e) {    
                System.out.println("error in wapaction,and e is " + e.getMessage());    
            }   
            System.out.println(res);  
            return res;    
        } 
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

如有什麼問題,請大家指正,謝謝!

  
  • 1
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            </div>