• java后台高德经纬度转地理位置信息


    import java.util.LinkedList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.config.RequestConfig;
    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;

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

    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 HOST = "http://122.114.79.163:7770/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 String mapDistanceMethod(String[] origins, String destination) {
    String responseEntity = null;
    for (int i = 0; i < 5 && responseEntity == null; i++) {
    responseEntity = mapDistance(origins, destination);
    }
    return responseEntity;
    }

    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;
    }

    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;
    }

    public static void main(String[] args) throws InterruptedException {
    //getLocationAddr("113.242439,35.1863620");
    System.out.println(getLocationAddr("113.242439,35.1863620"));
    // 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 < 100; i++) {
    // MapDistanceUtil.mapDistanceMethod(origins, destination);
    // System.out.println("---------第" + i + "次请求");
    // Thread.sleep(200);
    // }
    }
    }

  • 相关阅读:
    python爬虫实例--爬取拉勾网
    面试时,如何回答你还有什么想要了解的?
    深入理解三次握手四次挥手以及使用scapy实现ddos雏形
    解决socket粘包的两种low版模式 os.popen()和struct模块
    浅谈osi模型 三次握手 四次挥手 ddos攻击原理
    渲染相关书籍
    unity 场景编辑器物体加图标
    音乐模拟器
    3d服装制作软件
    uv投影插值
  • 原文地址:https://www.cnblogs.com/baihaojie/p/10218934.html
Copyright © 2020-2023  润新知