java实现高德地图经纬度转换成百度地图

因为项目中使用到了高德地图的API获取到了某查询位置的经纬度,但是页面定位的时候是使用的百度地图,所以这其中需要进行相应的转码。高德使用的是火星坐标(地球坐标加密后的地址),百度地图使用的是自己加密后的经纬度。


编写经纬度实体类

package com.liang.work.entity;

/**
 * Created by rcc on 2018/1/18.
 */
//经纬度实体类
public class Point {
    private double lon;
    private double lat;

    public Point(double lon, double lat) {
        this.lon = lon;
        this.lat = lat;
    }

    public double getLon() {
        return lon;
    }

    public void setLon(double lon) {
        this.lon = lon;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    @Override
    public String toString() {
        return "Point{" +
                "lon=" + lon +
                ", lat=" + lat +
                '}';
    }
}

编写主类

package com.liang.work.main;

import com.fasterxml.jackson.databind.JsonNode;
import com.liang.work.entity.Point;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import static java.lang.Math.sqrt;

/**
 * Created by rcc on 2018/1/17.
 */
public class ApplicationMain {
        private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
        private static double PI = 3.1415926535897932384626;

        static  double bd_lon = 0.0;
        static  double bd_lat = 0.0;
        public static void main(String [] args){
                String url = "http://restapi.amap.com/v3/geocode/geo?address=方恒国际中心A座&output=JSON&key=这里填写注册后获得的key值";
                AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
                builder.setCompressionEnabled(true).setAllowPoolingConnection(true);
                builder.setRequestTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));
                builder.setIdleConnectionTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));

                AsyncHttpClient client = new AsyncHttpClient(builder.build());
                try {
                        ListenableFuture<Response> future = client.prepareGet(url).execute();
                        String result = future.get().getResponseBody();
                        System.out.println(result);
                        JsonNode jsonNode = new com.fasterxml.jackson.databind.ObjectMapper().readTree(future.get().getResponseBody());
                        if(jsonNode.findValue("status").textValue().equals("1")) {
                                JsonNode listSource = jsonNode.findValue("location");
                                System.out.println(listSource);
                                for(String location : listSource.textValue().split(",")){
                                        //得到这个位置的经纬度
                                        System.out.println(location);

                                }
                                String[] location = listSource.textValue().split(",");
                                bd_lon = Double.parseDouble(location[0]);
                                bd_lat = Double.parseDouble(location[1]);
                                Point point = new Point(bd_lon,bd_lat);

                                Point point1 = bd_encrypt(point);
                                System.out.print(point1);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        if(client != null){
                                client.close();
                        }
                }
        }



        /**
         * 对double类型数据保留小数点后多少位
         *  高德地图转码返回的就是 小数点后6位,为了统一封装一下
         * @param digit 位数
         * @param in 输入
         * @return 保留小数位后的数
         */
        static double dataDigit(int digit,double in){
                return new BigDecimal(in).setScale(6,   BigDecimal.ROUND_HALF_UP).doubleValue();

        }

        /**
         * 将火星坐标转变成百度坐标
         * @param lngLat_gd 火星坐标(高德、腾讯地图坐标等)
         * @return 百度坐标
         */

        public static Point bd_encrypt(Point lngLat_gd)
        {
                double x = lngLat_gd.getLon(), y = lngLat_gd.getLat();
                double z = 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);
                return new Point(dataDigit(6,z * Math.cos(theta) + 0.0065),dataDigit(6,z * Math.sin(theta) + 0.006));

        }
}

最后出现的结果包含许多信息,可以参考高德的API说明:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/WJHelloWorld/article/details/79096726