puzhibing
2024-01-30 80b3ea5587ff7ec20541d9ca7c6c28739e4d615b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.dsh.guns.modular.system.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
public class GaoDeMapUtil {
 
 
 
    /**
     * 根据地址查询经纬度
     *
     * @param address
     * @return
     */
    public static JSONObject getLngAndLat(String address) {
        JSONObject positionObj = new JSONObject();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 拼接请求高德的url
            HttpGet httpget = new HttpGet(address+"&sensor=false&key=77b37f0753049c4e712ea79a24e0719c");
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
 
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容
                    String str = EntityUtils.toString(entity);
                    JSONObject o = (JSONObject) JSON.parse(str);
                    JSONArray o2 = (JSONArray) o.get("results");
                    JSONObject o3 =  (JSONObject) o2.get(0);
                    JSONObject o4 = (JSONObject) o3.get("geometry");
                    JSONObject o5 = (JSONObject)o4.get("location");
                    positionObj.put("longitude",o5.get("lng"));
                    positionObj.put("latitude",o5.get("lat"));
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return positionObj;
    }
 
 
 
    /**
     * 发送Get请求
     *
     * @param url
     * @return
     */
    public static String sendHttpGet(String url) {
        HttpGet httpGet = new HttpGet(url);
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setConnectTimeout(3000)
                .setSocketTimeout(10000)
                .build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        String result = "";
        try {
            CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpGet);
            HttpEntity entity = closeableHttpResponse.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}