WGS84轉騰訊地理座標系,並將經緯座標轉化爲騰訊地圖地址

項目後臺使用的座標系是WGS1984,但是小程序使用的是騰訊地圖。我想要將84座標轉化爲具體的地址存儲在數據庫中,供小程序使用。就需要先將84-》騰訊地圖使用的座標系-》騰訊地圖地址。

note:在網上找了很多實現的方法,大部分都沒有啥用,各種報錯,找到了比較好的是下面這種。

           如果有疑惑,可以查看文章底部的源代碼。


效果圖如下:


1.關於座標體系簡介如下。代碼大膽copy,一個都不用改。

   因爲感覺各種體系轉換總有一天能用上,所以就都沒刪,需要哪個用copy哪個嘛。

WGS:國際上通用的地心座標系。目前的設備包含GPS芯片獲取的經緯度一般爲WGS84地理座標系。

             谷歌衛星地圖使用的就是WGS-84標準。

GCJ-02:國家測繪局的一套標準GCJ-02(國測局),在WGS的基礎上進行加密偏移。

                 谷歌街道地圖、騰訊、高德地圖使用該標準。

BD-09:百度在GCJ-02的基礎上又進行了加密處理,形成了百度獨有的BD-09座標系。

package org.boot.city.utils;

/**
 * GPS84、高德、百度、騰訊編碼互轉
 */
public class LngLonUtil {

    public static double pi = 3.1415926535897932384626;
    public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    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 double[] transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{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 double[]{mgLat,mgLon};
    }
    /**
     * 判斷是否在中國
     * @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;
    }
    /**
     * 84 ==》 高德/騰訊
     * @param lat
     * @param lon
     * @return
     */
    public static double[] gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{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 double[]{mgLat, mgLon};
    }


    /**
     * 高德/騰訊 ==》 84
     * @param lon * @param lat * @return
     * */
    public static double[] gcj02_To_Gps84(double lat, double lon) {
        double[] gps = transform(lat, lon);
        double lontitude = lon * 2 - gps[1];
        double latitude = lat * 2 - gps[0];
        return new double[]{latitude, lontitude};
    }
    /**
     * 高德/騰訊 == 》 百度
     * @param lat
     * @param lon
     */
    public static double[] gcj02_To_Bd09(double lat, double lon) {
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta) + 0.0065;
        double tempLat = z * Math.sin(theta) + 0.006;
        double[] gps = {tempLat,tempLon};
        return gps;
    }

    /**
     * 百度 == 》 高德/騰訊
     * @param lat
     * @param lon
     */
    public static double[] bd09_To_Gcj02(double lat, double lon) {
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta);
        double tempLat = z * Math.sin(theta);
        double[] gps = {tempLat,tempLon};
        return gps;
    }

    /**
     * 84 == 》 百度
     * @param lat
     * @param lon
     * @return
     */
    public static double[] gps84_To_bd09(double lat,double lon){
        double[] gcj02 = gps84_To_Gcj02(lat,lon);
        double[] bd09 = gcj02_To_Bd09(gcj02[0],gcj02[1]);
        return bd09;
    }

    /**
     * 百度 == 》 84
     * @param lat
     * @param lon
     * @return
     */
    public static double[] bd09_To_gps84(double lat,double lon){
        double[] gcj02 = bd09_To_Gcj02(lat, lon);
        double[] gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1]);
        //保留小數點後六位
        gps84[0] = retain6(gps84[0]);
        gps84[1] = retain6(gps84[1]);
        return gps84;
    }

    /*
     * 保留小數點後六位
     * @param num
     * @return
     */
    private static double retain6(double num){
        String result = String .format("%.6f", num);
        return Double.valueOf(result);
    }
}

2.添加Gson依賴 

 <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

 

3.騰訊座標體系==》騰訊地圖地址

package org.boot.city.utils;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Map;


/**
 * 使用騰訊的接口獲取的地理位置。
 *
 */
public class GetLocationUtill {

    public static final String getAddr(double[] location) {
//        double lat = location.getLatitude();
//        double log = location.getLongitude();
        double lat = location[0];
        double log = location[1];
        double latlong[] = LngLonUtil.gps84_To_Gcj02(lat, log);
        StringBuilder sb = new StringBuilder();
        InputStream in = null;
        BufferedReader reader = null;
        try {
            URL url = new URL("http://apis.map.qq.com/jsapi?qt=rgeoc&lnglat=" + latlong[1] + "," + latlong[0]);
            in = url.openStream();
            reader = new BufferedReader(new InputStreamReader(in, "GBK"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            System.out.println("URL" + e.getMessage());
        } finally {
            try {
                in.close();
                reader.close();

            } catch (Exception e) {
            }
        }
        Map result = (Map) new Gson().fromJson(sb.toString(), Map.class);
        System.out.println(result);
        Map info = (Map) result.get("info");
        String error = info.get("error").toString();
        String addr = "";
        if (error.equals("0.0")) {
            Map detail = (Map) result.get("detail");
            List poilist = (List) detail.get("poilist");
            Map p1 = (Map) poilist.get(0);
            addr = (String) p1.get("addr");
            String name = (String) p1.get("name");
            addr = addr + name;
        }
        return addr;
    }

}

 

4.使用 

//我的經緯度座標p.getLat()是String類型,所以需要轉成double一下

Sring result = GetLocationUtill.getAddr(LngLonUtil.gps84_To_Gcj02(Double.parseDouble(p.getLat()),Double.parseDouble(p.getLng())));

 座標體系互轉參考地址:https://blog.csdn.net/qq_45829350/article/details/105053687?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

Gcj02體系==》騰訊地圖地址:https://blog.csdn.net/H517604180/article/details/81477920

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章