吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
package org.sxkj.common.utils;
 
 
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
/**
 * 高德地图根据地区名称获取经纬度
 */
public class AmapUtils {
 
    private static final String AMAP_KEY = "6c3ea75b215f0c0efcbcfdf13273991b";
    private static final String searchByKeywordUrl = "https://restapi.amap.com/v3/assistant/inputtips?&datatype=poi&key=" + AMAP_KEY + "&keywords=";
    private static final String poiKeywordUrl = "https://restapi.amap.com/v5/place/text?key=" + AMAP_KEY + "&keywords=";
    private static final String REGEO_URL = "https://restapi.amap.com/v3/geocode/regeo?output=json&key="+AMAP_KEY+"&radius=0&extensions=base&location=";
 
    public static JSONObject searchByKeyword(String keyword) {
 
        JSONObject result = apiRequestResult(searchByKeywordUrl + keyword);
        return result;
    }
 
    public static JSONObject poiKeywordUrl(String keyword) {
 
        JSONObject result = apiRequestResult(poiKeywordUrl + keyword);
        return result;
    }
 
 
    public static JSONObject searchByLatLng(String location) {
 
        JSONObject result = apiRequestResult(REGEO_URL + location);
        return result;
    }
 
    private static JSONObject apiRequestResult(String url) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        //封装请求头
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
        try {
            RestTemplate restTemplate = SpringContextUtil.getBean(RestTemplate.class);
            //有请求头,有参数请求
            ResponseEntity<String> responseEntity =
                restTemplate.exchange(url,
                    HttpMethod.GET,
                    formEntity,
                    String.class);
            String body = responseEntity.getBody();
 
            JSONObject parse = JSONObject.parseObject(body);
 
            // 返回
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}