package cab.bear.util;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 高德地图工具
*/
@Component
public class AMapUtil {
@Value("${amap.key}")
private String key; // 高德地图开发者 key
/**
* 关键字搜索地址
* @param keywords
* @return
* @throws MalformedURLException
* @throws URISyntaxException
*/
public JSONArray search(String keywords) throws MalformedURLException, URISyntaxException {
String searchUrl = "https://restapi.amap.com/v5/place/text?key=" + key + "&keywords=" + keywords;
URL url = new URL(searchUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
String content = EntityUtils.toString(response.getEntity());
JSONObject parseObject = JSON.parseObject(content);
String status = parseObject.getString("status");
if("1".equals(status)) {
JSONArray pois = parseObject.getJSONArray("pois");
return pois;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 关键字搜索地址
* @param region
* @param keywords
* @return
* @throws MalformedURLException
* @throws URISyntaxException
*/
public JSONArray search(String region ,String keywords) throws MalformedURLException, URISyntaxException {
String searchUrl = "https://restapi.amap.com/v5/place/text?key=" + key + "&keywords=" + keywords + "&citylimit=true®ion=" + region; // region 搜索区域,默认全国范围;citylimit:仅返回指定城市数据,默认false
URL url = new URL(searchUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
String content = EntityUtils.toString(response.getEntity());
JSONObject parseObject = JSON.parseObject(content);
String status = parseObject.getString("status");
if("1".equals(status)) {
JSONArray pois = parseObject.getJSONArray("pois");
return pois;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 地址转换为经纬度
* @param address
* @return 经纬度
* @throws MalformedURLException
* @throws URISyntaxException
*/
public JSONArray getLonLat(String address) throws MalformedURLException, URISyntaxException {
String lonLatUrl = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&batch=true&address=" + address;
URL url = new URL(lonLatUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
String content = EntityUtils.toString(response.getEntity());
JSONObject parseObject = JSON.parseObject(content);
String status = parseObject.getString("status");
if("1".equals(status)) {
JSONArray regeocodes = parseObject.getJSONArray("geocodes");
return regeocodes;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 根据两个定位点的经纬度获取两点之间驾车路径规划
* @throws MalformedURLException
* @throws URISyntaxException
*/
public JSONObject getDriving(String originLonLat, String destinationLonLat,int strategy) throws MalformedURLException, URISyntaxException {
String drivingUrl = "https://restapi.amap.com/v3/direction/driving?key=" + key + "&strategy=" + strategy + "&origin=" + originLonLat + "&destination=" + destinationLonLat;
URL url = new URL(drivingUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
String content = EntityUtils.toString(response.getEntity());
JSONObject parseObject = JSON.parseObject(content);
String status = parseObject.getString("status");
if("1".equals(status)) {
JSONObject jsonObject = parseObject.getJSONObject("route");
return jsonObject;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 根据两个地址关键字获取两点之间驾车路径规划
* @throws URISyntaxException
* @throws MalformedURLException
*/
public JSONObject getAddressDriving(String originAddress, String destinationAddress, int strategy) throws MalformedURLException, URISyntaxException {
JSONArray lonLat = getLonLat(originAddress + "|" + destinationAddress);
if(lonLat != null) {
return getDriving(lonLat.getJSONObject(0).getString("location"), lonLat.getJSONObject(1).getString("location"), strategy);
}
return null;
}
/**
* 行政区域查询
* @throws MalformedURLException
* @throws URISyntaxException
*/
public JSONArray getDistricts(Integer subdistrict) throws MalformedURLException, URISyntaxException {
// https://restapi.amap.com/v3/config/district?subdistrict=3 # subdistrict: 0:不返回下级行政区;1:返回下一级行政区;2:返回下两级行政区;3:返回下三级
String districtUrl = "https://restapi.amap.com/v3/config/district?key=" + key + "&subdistrict=" + subdistrict;
URL url = new URL(districtUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity());
JSONObject parseObject = JSON.parseObject(content);
String status = parseObject.getString("status");
if("1".equals(status)) {
JSONArray array = parseObject.getJSONArray("districts");
for(int n = 0; n < array.size(); n++) {
JSONObject district = array.getJSONObject(n);
if("country".equals(district.getString("level")) && "100000".equals(district.getString("adcode"))) {
// 中华人民共和国行政区域
JSONArray districts = district.getJSONArray("districts");
return districts;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) throws MalformedURLException, URISyntaxException {
String districtUrl = "https://restapi.amap.com/v3/config/district?key=6ebe75ea77cd6f178e3073de21a57a6e&subdistrict=3";
URL url = new URL(districtUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity());
JSONObject parseObject = JSON.parseObject(content);
String status = parseObject.getString("status");
if("1".equals(status)) {
JSONArray array = parseObject.getJSONArray("districts");
for(int n = 0; n < array.size(); n++) {
JSONObject district = array.getJSONObject(n);
if("country".equals(district.getString("level")) && "100000".equals(district.getString("adcode"))) {
// 中华人民共和国行政区域
JSONArray districts = district.getJSONArray("districts");
districts.toJSONString();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}