高德地图获取经纬度、高德地图坐标转为百度地图坐标

通过异步网络请求,调用高德API,获取某个地址的经纬度等信息,再转为百度的经纬度。高德地图API,点击这里

package com.alibaba.controller;

import com.alibaba.entity.MyPoint;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
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 org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * Created by lightClouds917
 * Date 2018/1/17
 * Description:异步网络请求
 * 此处以请求高德地图示例
 */
@RestController
@RequestMapping("asynClient")
public class AsynClientController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
    /** π */
    private static double PI = 3.1415926535897932384626;
    /** 参数 */
    private static double X_PI = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * 高德地图  获取经纬度
     * @return
     */
    @RequestMapping(value = "test6",method = RequestMethod.GET)
    public String test6() {
        logger.info("==================>查询高德地图");

        String url  = "http://restapi.amap.com/v3/geocode/geo?address=方恒国际中心A座&key=05831d6b4be84db6d042b1d00d65b006";
        String url1 = "http://restapi.amap.com/v3/geocode/geo?address=北京市朝阳区阜通东大街6号&output=XML&key=05831d6b4be84db6d042b1d00d65b006";
        String url2  = "http://restapi.amap.com/v3/geocode/geo?address=杭州市江干区杭海路1099号&key=05831d6b4be84db6d042b1d00d65b006";

        //异步网络请求
        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 future = client.prepareGet(url2).execute();
            String result = future.get().getResponseBody();
            logger.info("======>result:"+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");
                for (String location : listSource.textValue().split(",")) {
                    //得到这个位置的经纬度
                    logger.info(location);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }

    /**
     * 查询高德地图坐标
     * 转为百度地图
     * url中:address为查询地址
     *       key需要去高德地图申请,类似于秘钥一样 ,申请地址:http://lbs.amap.com/api/webservice/summary/
     */
    @RequestMapping(value = "test7",method = RequestMethod.GET)
    public void test7(){
        try {
            logger.info("==================>查询高德地图");
            String url  = "http://restapi.amap.com/v3/geocode/geo?address=方恒国际中心A座&key=05831d6b4be84db6d042b1d00d65b006";
            //异步网络请求   比上面的简化了一下
            AsyncHttpClient client = new AsyncHttpClient();
            AsyncHttpClient.BoundRequestBuilder builder = client.prepareGet(url);
            ListenableFuture future = builder.execute();

            JsonNode jsonNode = new ObjectMapper().readTree(future.get().getResponseBody());
            if(jsonNode.findValue("status").textValue().equals("1")){
                JsonNode location = jsonNode.findValue("location");;
                logger.info("==========>location:"+location);
                String[] lonAndLat = location.textValue().split(",");
                logger.info("========>经度:"+lonAndLat[0]);
                logger.info("========>纬度:"+lonAndLat[1]);

                //高德---->百度地图
                MyPoint myPoint = bd_encrypt(new MyPoint(Double.valueOf(lonAndLat[0]), Double.valueOf(lonAndLat[1])));
                logger.info("========>经度:"+myPoint.getLon());
                logger.info("========>纬度:"+myPoint.getLat());
            }
            String responseBody = future.get().getResponseBody();
            logger.info(responseBody);
        }catch (Exception ex){
            ex.printStackTrace();
            logger.info("=======>查询出错",ex);
        }
    }

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

    }

    /**
     * 将火星坐标转变成百度坐标
     * @param lngLat_gd 火星坐标(高德、腾讯地图坐标等)
     * @return 百度坐标
     */
    public static MyPoint bd_encrypt(MyPoint lngLat_gd) {
        double x = lngLat_gd.getLon(), y = lngLat_gd.getLat();
        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);
        return new MyPoint(dataDigit(6,z * Math.cos(theta) + 0.0065),dataDigit(6,z * Math.sin(theta) + 0.006));
    }
}


你可能感兴趣的:((25)...Spring,Boot,(16)...技术资料)