2021-07-12 14:51:34 +08:00
|
|
|
package com.xkrs.utils;
|
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
import io.micrometer.core.instrument.util.StringUtils;
|
2021-07-12 14:51:34 +08:00
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
import javax.net.ssl.*;
|
|
|
|
import java.io.*;
|
2021-07-12 14:51:34 +08:00
|
|
|
import java.net.URL;
|
2021-07-13 10:18:08 +08:00
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
import java.security.cert.X509Certificate;
|
2021-07-12 14:51:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据经纬度获取地址:省 市 区 位置名称
|
|
|
|
* @author XinYi Song
|
|
|
|
*/
|
|
|
|
public class AddressUtils {
|
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
public static String getLatAndLng(String lat, String lng) {
|
|
|
|
String key = "O7QBZ-ZYDKI-EMKGN-53UHG-5XSJF-AAFBP";
|
2021-07-12 14:51:34 +08:00
|
|
|
try {
|
2021-07-13 10:18:08 +08:00
|
|
|
String hsUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key + "&get_poi=1";
|
2021-07-12 14:51:34 +08:00
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
URL url;
|
2021-07-12 14:51:34 +08:00
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
url = new URL(hsUrl);
|
|
|
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
|
|
|
// 提交模式
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
X509TrustManager xtm = new X509TrustManager() {
|
|
|
|
@Override
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-12 14:51:34 +08:00
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
@Override
|
|
|
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
|
|
|
|
throws CertificateException {
|
|
|
|
// TODO Auto-generated method stub
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
2021-07-13 10:18:08 +08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
|
|
|
|
throws CertificateException {
|
|
|
|
// TODO Auto-generated method stub
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
2021-07-13 10:18:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TrustManager[] tm = {xtm};
|
|
|
|
|
|
|
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
|
|
|
ctx.init(null, tm, null);
|
|
|
|
|
|
|
|
con.setSSLSocketFactory(ctx.getSocketFactory());
|
|
|
|
con.setHostnameVerifier(new HostnameVerifier() {
|
|
|
|
@Override
|
|
|
|
public boolean verify(String arg0, SSLSession arg1) {
|
|
|
|
return true;
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
2021-07-13 10:18:08 +08:00
|
|
|
});
|
2021-07-12 14:51:34 +08:00
|
|
|
|
|
|
|
|
2021-07-13 10:18:08 +08:00
|
|
|
InputStream inStream = con.getInputStream();
|
|
|
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int len = 0;
|
|
|
|
while ((len = inStream.read(buffer)) != -1) {
|
|
|
|
outStream.write(buffer, 0, len);
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
2021-07-13 10:18:08 +08:00
|
|
|
//网页的二进制数据
|
|
|
|
byte[] b = outStream.toByteArray();
|
|
|
|
outStream.close();
|
|
|
|
inStream.close();
|
|
|
|
String rtn = new String(b, "utf-8");
|
|
|
|
if (StringUtils.isNotBlank(rtn)) {
|
|
|
|
JSONObject object = JSONObject.fromObject(rtn);
|
|
|
|
if (object != null) {
|
|
|
|
if (object.has("status") && object.getInt("status") == 0) {
|
|
|
|
JSONObject result = JSONObject.fromObject(object.get("result"));
|
|
|
|
if (result != null) {
|
|
|
|
JSONObject addressComponent = JSONObject.fromObject(result.get("address_component"));
|
|
|
|
if (addressComponent != null) {
|
|
|
|
String province = (String) addressComponent.get("province");
|
|
|
|
String city = (String) addressComponent.get("city");
|
|
|
|
String district = (String) addressComponent.get("district");
|
|
|
|
String street = (String) addressComponent.get("street");
|
|
|
|
String street_number = (String) addressComponent.get("street_number");
|
|
|
|
String address = province + city + district + street + street_number;
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
2021-07-13 10:18:08 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
2021-07-13 10:18:08 +08:00
|
|
|
return null;
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2021-07-14 09:23:53 +08:00
|
|
|
String latAndLng = getLatAndLng("36.677841", "119.12423");
|
2021-07-13 10:18:08 +08:00
|
|
|
System.out.println(latAndLng);
|
2021-07-12 14:51:34 +08:00
|
|
|
}
|
|
|
|
}
|