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 {
|
|
// public static void main(String[] args) {
|
// // 地址名称
|
// String address = "深圳市";
|
// // 调用自己写好的封装方法
|
// JSONObject positionObj = getLngAndLat(address);
|
// String longitude = positionObj.getString("longitude");
|
// String latitude = positionObj.getString("latitude");
|
// System.out.println("经度:" + longitude);
|
// System.out.println("纬度:" + latitude);
|
//
|
// }
|
|
/**
|
* 根据地址查询经纬度
|
*
|
* @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;
|
}
|
}
|