高德逆地理编码

import java.net.URI;
import java.util.LinkedList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 公共路线接口方法,计算实际距离。
 */

/**
 * java后台高德经纬度转地理位置信息
 */
@Slf4j
public class GaodeUtil {

    private static final String HOSTARR = "http://restapi.amap.com/v3/geocode/regeo";
    private static final String HOST = "https://restapi.amap.com/v3/distance";
    private static final String KEY = "761e73b6188fb868feee5b558dcbcd7a";

    /**
     * 参数:(出发点数组、目的点) 格式:经度,纬度
     * 返回: {
     * "status": "1",
     * "info": "OK",
     * "infocode": "10000",
     * "results": [{ //结果数组
     * "origin_id": "1", //第一个起点
     * "dest_id": "1", //目的地
     * "distance": "261278", //距离(米)
     * "duration": "14280" //预计时间(秒)
     * }]
     * }
     */

    public static void main(String[] args) throws InterruptedException {
//        getLocationAddr("113.242439,35.1863620");
//        System.out.println(getLocationAddr("119.215091,36.723652"));
//        System.out.println("************************************");
        System.out.println(getAddress("116.481028,39.989643"));

//        String[] origins = new String[]{"116.481028,39.989643", "114.481028,39.989643", "115.481028,39.989643"};
//        String destination = "114.465302,40.004717";
//        for (int i = 0; i < 2; i++) {
//            String s = GaodeUtil.mapDistanceMethod(origins, destination);
//            System.out.println(s);
//            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(s);
//            for (int j = 0; j < jsonObject.getJSONArray("results").size(); j++){
//                String str = jsonObject.getJSONArray("results").getJSONObject(j).get("distance").toString();
//                System.out.println(str);
//            }
//
//            System.out.println("---------第" + i + "次请求");
//            Thread.sleep(200);
//        }
    }

    private static String getAddress(String address) {
        LinkedList<NameValuePair> list = new LinkedList<>();
        String ad = "";
        BasicNameValuePair key = new BasicNameValuePair("key", KEY);
        list.add(key);
        list.add(new BasicNameValuePair("output", KEY));
        list.add(new BasicNameValuePair("location", address));
        try {
            URI build = new URIBuilder(HOSTARR).setParameters(list).build();
            HttpGet httpGet = new HttpGet(build);
            httpGet.setConfig(RequestConfig.custom().setConnectTimeout(2000).build());
            CloseableHttpResponse execute = HttpClients.createDefault().execute(httpGet);
            String s = EntityUtils.toString(execute.getEntity(), "UTF-8");
            JSONObject jsonObject = JSON.parseObject(s);
            ad = jsonObject.getJSONObject("regeocode").getString("formatted_address");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ad;
    }

    public static String getLocationAddr(String location) {
        List<NameValuePair> list = new LinkedList<>();
        list.add(new BasicNameValuePair("key", KEY));
        list.add(new BasicNameValuePair("output", "JSON"));
        list.add(new BasicNameValuePair("location", location));
        String responseEntity = null;
        String addr = "";
        try {
            HttpGet httpGet = new HttpGet(new URIBuilder(HOSTARR).setParameters(list).build());
            httpGet.setConfig(RequestConfig.custom().setConnectTimeout(2000).build());
            HttpResponse response = HttpClients.createDefault().execute(httpGet);
            responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
            JSONObject jsonObject = JSON.parseObject(responseEntity);
            addr = jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent")
                    .getJSONObject("building").getString("name");
            if (addr.equals("[]")) {
                addr = jsonObject.getJSONObject("regeocode").getString("formatted_address");
                if (addr.indexOf("省") > 0) {
                    addr = addr.substring(addr.indexOf("省") + 1, addr.length());
                }
                if (addr.indexOf("市") > 0) {
                    addr = addr.substring(addr.indexOf("市") + 1, addr.length());
                }
            }
//System.out.println("-------响应结果------- " + addr);
        } catch (Exception e) {
            if (e instanceof ConnectTimeoutException) {
                System.out.println("-------请求超时-------");
            } else {
                e.printStackTrace();
            }
        }
        return addr;
    }

    public static String mapDistanceMethod(String[] origins, String destination) {
        String responseEntity = null;
        for (int i = 0; i < 5 && responseEntity == null; i++) {
            responseEntity = mapDistance(origins, destination);
        }
        return responseEntity;
    }

    private static String mapDistance(String[] origins, String destination) {
        StringBuilder originBuilder = new StringBuilder();
        for (int i = 0; i < origins.length; i++) {
            originBuilder.append(origins[i]);
            if (i < origins.length - 1) {
                originBuilder.append("|");
            }
        }
        List<NameValuePair> list = new LinkedList<>();
        list.add(new BasicNameValuePair("key", KEY));
        list.add(new BasicNameValuePair("output", "JSON"));
        list.add(new BasicNameValuePair("origins", originBuilder.toString()));
        list.add(new BasicNameValuePair("destination", destination));
        String responseEntity = null;
        try {
            HttpGet httpGet = new HttpGet(new URIBuilder(HOST).setParameters(list).build());
            httpGet.setConfig(RequestConfig.custom().setConnectTimeout(2000).build());
            HttpResponse response = HttpClients.createDefault().execute(httpGet);
            responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
//System.out.println("-------距离测量响应结果------- " + responseEntity);
        } catch (Exception e) {
            if (e instanceof ConnectTimeoutException) {
                System.out.println("-------请求超时-------");
            } else {
                e.printStackTrace();
            }
        }
        return responseEntity;
    }


}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 12:05
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 12:04
毕业生招你惹你了,问一个发薪日来一句别看网上乱七八糟的你看哪个工作没有固定发薪日扭头就取消了面试就问了一句公司都是这个态度吗还搞上人身攻击了...
程序员小白条:呃呃呃,都还没面试,我都不会问这么细,何况通不通过,去不去都另说,你没实力和学历的话,在外面就这样,说实话没直接已读不回就不错了,浪费时间基本上
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 13:35
虽然不怎么光彩,经过这件事,可能我真的要去认同“面试八股文早该淘汰!不会用AI作弊的程序员=新时代文盲!”这句话了
HellowordX:Ai的出现是解放劳动力的,不是用来破坏公平竞争环境的,这样下去,轻则取消所有线上面试,严重了会影响整个行业对所有人产生影响,企业会拉高入职考核各种离谱考核会层出不穷
你找工作的时候用AI吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务