java地图坐标转换

实现代码

public class gpsUtil {
    private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * GCJ02腾讯地图坐标转BD09百度坐标
     * */
    public static double[] TXToBD(int lat,int lng){

        double z =Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
        double toLng =  (z * Math.cos(theta) + 0.0065);
        double toLat = (z * Math.sin(theta) + 0.006);

        double gps[]= new double[2];

        gps[0] = toLat;
        gps[1] = toLng;
        return gps;
    }
    /**
     * 百度地图BD09坐标---->中国正常GCJ02坐标,腾讯地图用的也是GCJ02坐标
     * */
    public static double[] BDToTX(double lat,double lng){
        double x = lng - 0.0065;
        double 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 toLng = z * Math.cos(theta);

        double toLat = z * Math.sin(theta);

        double gps[]= new double[2];
        gps[0] = toLat;
        gps[1] = toLng;
        return gps;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中常用的地图坐标加密方法是将经纬度坐标转换为一个短字符串,以保护用户隐私和数据安全。其中,最常用的加密方法是Geohash算法。 Geohash算法将经纬度坐标编码为一系列二进制数字,然后将其转换为一个短字符串。这个字符串越长,表示的精度越高。一般情况下,6~12位的Geohash字符串就可以满足绝大部分的应用。 以下是Java中使用Geohash算法将经纬度坐标加密为短字符串的示例代码: ```java import ch.hsr.geohash.GeoHash; public class GeoHashUtil { /** * 将经纬度坐标加密为Geohash字符串 * * @param latitude 纬度 * @param longitude 经度 * @param length Geohash字符串长度 * @return Geohash字符串 */ public static String encode(double latitude, double longitude, int length) { GeoHash geoHash = GeoHash.withCharacterPrecision(latitude, longitude, length); return geoHash.toBase32(); } /** * 将Geohash字符串解密为经纬度坐标 * * @param geohash Geohash字符串 * @return 经纬度坐标数组([0]为纬度,[1]为经度) */ public static double[] decode(String geohash) { GeoHash geoHash = GeoHash.fromGeohashString(geohash); double[] location = new double[2]; location[0] = geoHash.getBoundingBox().getCenter().getLatitude(); location[1] = geoHash.getBoundingBox().getCenter().getLongitude(); return location; } } ``` 使用示例: ```java public static void main(String[] args) { double latitude = 39.9042; double longitude = 116.4074; String geohash = GeoHashUtil.encode(latitude, longitude, 6); System.out.println("Geohash: " + geohash); double[] location = GeoHashUtil.decode(geohash); System.out.println("Latitude: " + location[0] + ", Longitude: " + location[1]); } ``` 输出结果: ``` Geohash: wx4g0g Latitude: 39.906005859375, Longitude: 116.40625 ``` 需要使用Geohash算法的话,可以引入ch.hsr.geohash库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一曲末

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值