坐标系转换网站:

https://tool.lu/zh_CN/coordinate/

利用该网站封装的java代码:

    //gps坐标转换 wgs84 bd09 gcj02
    public void postGPS(String src_type, double lat, double lng, XConnect.PostGetInfoListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                OkHttpClient client = new OkHttpClient();

                MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
                RequestBody body = RequestBody.create(mediaType, "src_type="+src_type+"&src_coordinate="+lng+"%2C"+lat+"&src_band=");

                Request request = new Request.Builder()
                        .url("https://tool.lu/zh_CN/coordinate/ajax.html?mode=single")
                        .post(body)
                        .addHeader("authority", "tool.lu")
                        .addHeader("accept", "application/json, text/javascript, */*; q=0.01")
                        .addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
                        .addHeader("origin", "https://tool.lu")
                        .addHeader("referer", "https://tool.lu/zh_CN/coordinate/?ivk_sa=1024320u")
                        .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
                        .addHeader("x-requested-with", "XMLHttpRequest")
                        .build();

                Response response = client.newCall(request).execute();
                    String resultText = response.body().string();
                    Log.i("Connect", "--> POST "+"\r\n"+" "+resultText);
                    handler_main.post(new Runnable() {
                        @Override
                        public void run() {
                            listener.onPostGetText(resultText);

                        }
                    });
                } catch (IOException e) {
                    handler_main.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.i("Connect", "--> POST "+"\r\n"+" error ");
                            listener.onPostGetText("");
                        }
                    });

                }
            }
        }).start();
    }

坐标系转换(百度API):

https://lbsyun.baidu.com/faq/api?title=webapi/guide/changeposition-base
对百度API进行的封装:https://www.yzjlb.net/game/baidumap/mapapi.php

坐标系转换(Java代码):


//import com.unicom.entity.TransformPoint;

import android.os.Build;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * 坐标系转换工具类 *
 */
public class CoordinateTransferUtils {
    /**
     * 国内坐标边界
     */
    private static final double MIN_LON = 72.004D;
    private static final double MAX_LON = 137.8347D;
    private static final double MIN_LAT = 0.8293D;
    private static final double MAX_LAT = 55.8271D;

    /**
     * PI 圆周率
     */
    private static final double PI = 3.14159265358979324D;

    /**
     * A WGS 长轴半径
     */
    private static final double A = 6378245.0D;

    /**
     * EE WGS 偏心率的平方
     */
    private static final double EE = 0.00669342162296594323D;

    /**
     * WGS84转换GCJ02适用于继承的情况
     *
     * @param t 继承TransformPoint类的子类对象
     */
//    public static <T extends TransformPoint> void wgs84ToGcj02(T t) {
//        wgs84ToGcj02(t::getLng, t::getLat, t::setTransformLng, t::setTransformLat);
//    }

    /**
     * WGS84转换GCJ02核心方法
     *
     * @param fromLon 转换前的经度
     * @param fromLat 转换前的纬度
     * @return 转换后的经纬度map对象
     */
    public static Map<String, Double> wgs84ToGcj02(double fromLon, double fromLat) {
        HashMap<String, Double> transformRes = new HashMap<>(2);
        // 国外坐标不用进行加密
        if (outOfChina(fromLon, fromLat)) {
            transformRes.put("lon", fromLon);
            transformRes.put("lat", fromLat);
            return transformRes;
        }
        // 计算转换后的经纬度坐标
        double dLat = transformLat(fromLon - 105.0, fromLat - 35.0);
        double dLon = transformLon(fromLon - 105.0, fromLat - 35.0);
        double radLat = fromLat / 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 = fromLat + dLat;
        double mgLon = fromLon + dLon;
        transformRes.put("lon", new BigDecimal(mgLon + "", new MathContext(9, RoundingMode.HALF_UP)).doubleValue());
        transformRes.put("lat", new BigDecimal(mgLat + "", new MathContext(9, RoundingMode.HALF_UP)).doubleValue());

        return transformRes;
    }

    /**
     * GCJ02转换WGS84
     *
     * @param lon 转换前的经度
     * @param lat 转换后的纬度
     * @return 转换后的经纬度map对象
     */
    public static Map<String, Double> gcj02ToWgs84(double lon, double lat) {
        Map<String, Double> transformRes = new HashMap<>(2);
        double longitude = lon * 2 - wgs84ToGcj02(lon, lat).get("lon");
        double latitude = lat * 2 - wgs84ToGcj02(lon, lat).get("lat");
        transformRes.put("lon", longitude);
        transformRes.put("lat", latitude);
        return transformRes;
    }

    /**
     * GCJ02转换BD09
     *
     * @param gcjLat GCL纬度坐标
     * @param gcjLng GCL经度坐标
     */
    public static Map<String, Double> Gcj02ToBd09(double gcjLat, double gcjLng) {
        Map<String, Double> transformRes = new HashMap<>(2);
        double x_pi = PI * 3000.0 / 180.0;
        double z = Math.sqrt(gcjLng * gcjLng + gcjLat * gcjLat) + 0.00002 * Math.sin(gcjLat * x_pi);
        double theta = Math.atan2(gcjLat, gcjLng) + 0.000003 * Math.cos(gcjLng * x_pi);
        transformRes.put("lon", z * Math.cos(theta) + 0.0065);
        transformRes.put("lat", z * Math.sin(theta) + 0.006);
        return transformRes;

    }

    /**
     * BD09转换GCJ02
     *
     * @param bdLat 百度纬度坐标
     * @param bdLng 百度经度坐标
     */
    public static Map<String, Double> bd09ToGcj02(double bdLat, double bdLng) {
        Map<String, Double> transformRes = new HashMap<>(2);
        double x = bdLng - 0.0065, y = bdLat - 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);

        transformRes.put("lon", z * Math.cos(theta));
        transformRes.put("lat", z * Math.sin(theta));
        return transformRes;
    }

    /**
     * WGS84转换GCJ02优化(处理转换前经纬度坐标为null问题)
     *
     * @param fromLon 转换前的经度
     * @param fromLat 转换前的纬度
     * @param toLon   转换后的经度
     * @param toLat   转换后的纬度
     */
//    private static void wgs84ToGcj02(Supplier<BigDecimal> fromLon, Supplier<BigDecimal> fromLat, Consumer<BigDecimal> toLon, Consumer<BigDecimal> toLat) {
//        wgs84ToGcj02(Optional.ofNullable(fromLon.get()).orElse(BigDecimal.ZERO), Optional.ofNullable(fromLat.get()).orElse(BigDecimal.ZERO), toLon, toLat);
//    }

    /**
     * WGS84转换GCJ02优化(处理转换后经纬度坐标)
     *
     * @param fromLon 转换前的经度
     * @param fromLat 转换前的纬度
     * @param toLon   转换后的经度
     * @param toLat   转换后的纬度
     */
    private static void wgs84ToGcj02(BigDecimal fromLon, BigDecimal fromLat, Consumer<BigDecimal> toLon, Consumer<BigDecimal> toLat) {
        final Map<String, Double> transformRes = wgs84ToGcj02(fromLon.doubleValue(), fromLat.doubleValue());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            toLon.accept(new BigDecimal(transformRes.get("lon") + ""));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            toLat.accept(new BigDecimal(transformRes.get("lat") + ""));
        }
    }

    /**
     * WGS84坐标系转Bd09坐标系
     *
     * @param fromLon 转换前的经度
     * @param fromLat 转换前的纬度
     * @return 转换后的经纬度map对象
     */
    public static Map<String, Double> wgs84ToBd09(double fromLon, double fromLat){
        Map<String, Double> Gcj02 = wgs84ToGcj02(fromLon,fromLat);
        Map<String, Double> Bd09 = CoordinateTransferUtils.Gcj02ToBd09(Gcj02.get("lat"),Gcj02.get("lon"));
        return Bd09;
    }

    /**
     * 坐标是否在国外
     *
     * @param lon 经度
     * @param lat 纬度
     * @return true 国外坐标 false 国内坐标
     */
    private static boolean outOfChina(double lon, double lat) {
        if (lon < MIN_LON || lon > MAX_LON) {
            return true;
        }
        return lat < MIN_LAT || lat > MAX_LAT;
    }

    /**
     * 转换经度坐标
     *
     * @param x 偏移后的经度
     * @param y 偏移后的纬度
     * @return double 转换经度坐标
     */
    private static double transformLat(double x, double y) {
        double transform = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        transform += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        transform += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        transform += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return transform;
    }

    /**
     * 转换纬度坐标
     *
     * @param x 偏移后的经度
     * @param y 偏移后的纬度
     * @return double 转换纬度坐标
     */
    private static double transformLon(double x, double y) {
        double transform = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        transform += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        transform += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        transform += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return transform;
    }

    /**
     * 坐标系转换工具类测试
     *
     * @param args 参数
     */
    public static void main(String[] args) {

        // WGS84ToGCJ02 116.404, 39.915 转换后 [ 116.41024449916938, 39.91640428150164 ]
        double lon3 = 116.404D;
        double lat3 = 39.915D;
        Map<String, Double> transform3Result = wgs84ToGcj02(lon3, lat3);
        System.out.println("WGS84ToGCJ02 转换前经纬度(" + lon3 + "," + lat3 + ") 转换后经纬度(" + transform3Result.get("lon") + "," + transform3Result.get("lat") + ")");

        // GCJ02ToWGS84 116.404, 39.915 转换后 [ 116.39775550083061, 39.91359571849836 ]
//        double lon4 = 116.404D;
//        double lat4 = 39.915D;
//        Map<String, Double> transform4Result = gcj02ToWgs84(lon4, lat4);
//        System.out.println("GCJ02ToWGS84 转换前经纬度(" + lon4 + "," + lat4 + ") 转换后经纬度(" + transform4Result.get("lon") + "," + transform4Result.get("lat") + ")");
//
//        // BD09ToGCJ02 116.404, 39.915 转换后 [ 116.39762729119315, 39.90865673957631 ]
//        double lon5 = 116.404D;
//        double lat5 = 39.915D;
//        Map<String, Double> transform5Result = bd09ToGcj02(lon5, lat5);
//        System.out.println("BD09ToGCJ02 转换前经纬度(" + lon5 + "," + lat5 + ") 转换后经纬度(" + transform5Result.get("lon") + "," + transform5Result.get("lat") + ")");

        // GCJ02ToBD09 116.404, 39.915 转换后 [ 116.41036949371029, 39.92133699351021 ]
//        double lon6 = 116.404D;
//        double lat6 = 39.915D;
//        Map<String, Double> transform6Result = gcj02ToBd09(lon6, lat6);
//        System.out.println("GCJ02ToBD09 转换前经纬度(" + lon6 + "," + lat6 + ") 转换后经纬度(" + transform6Result.get("lon") + "," + transform6Result.get("lat") + ")");

    }
}

使用腾讯地图网页加载GPS坐标点进行位置展示:

webview.loadUrl("https://apis.map.qq.com/tools/poimarker?type=0&marker=coord%3A"+latitude+"%2C"+longitude+"%3Btitle%3A当前位置&referer=myapp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77");

发表评论

邮箱地址不会被公开。 必填项已用*标注