WGS84转腾讯地理坐标系,并将经纬坐标转化为腾讯地图地址

项目后台使用的坐标系是WGS1984,但是小程序使用的是腾讯地图。我想要将84坐标转化为具体的地址存储在数据库中,供小程序使用。就需要先将84-》腾讯地图使用的坐标系-》腾讯地图地址。

note:在网上找了很多实现的方法,大部分都没有啥用,各种报错,找到了比较好的是下面这种。

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


效果图如下:

WGS84转腾讯地理坐标系,并将经纬坐标转化为腾讯地图地址_第1张图片


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依赖 

 
        
            com.google.code.gson
            gson
            2.8.6
        

 

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

你可能感兴趣的:(java,地图,java)