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