packagecom.weixin.map;importnet.sf.json.JSONObject;importorg.apache.commons.lang.StringUtils;import javax.net.ssl.*;importjava.io.ByteArrayOutputStream;importjava.io.InputStream;importjava.net.URL;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;public classTest {/*** 通过腾讯地图将经纬度转成详细地址
*
*@paramlat 纬度
*@paramlng 经度
*@paramkey 开发密钥(Key) 需自行申请
*@return
*/
public staticJSONObject getLatAndLng(String lat, String lng, String key) {try{
String hsUrl= "https://apis.map.qq.com/ws/geocoder/v1/?location="+lat+","+lng+"&key="+key+"&get_poi=1";
URL url;
url= newURL(hsUrl);
HttpsURLConnection con=(HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");//提交模式
X509TrustManager xtm = newX509TrustManager() {
@OverridepublicX509Certificate[] getAcceptedIssuers() {//TODO Auto-generated method stub
return null;
}
@Overridepublic voidcheckServerTrusted(X509Certificate[] arg0, String arg1)throwsCertificateException {//TODO Auto-generated method stub
}
@Overridepublic voidcheckClientTrusted(X509Certificate[] arg0, String arg1)throwsCertificateException {//TODO Auto-generated method stub
}
};
TrustManager[] tm={xtm};
SSLContext ctx= SSLContext.getInstance("TLS");
ctx.init(null, tm, null);
con.setSSLSocketFactory(ctx.getSocketFactory());
con.setHostnameVerifier(newHostnameVerifier() {
@Overridepublic booleanverify(String arg0, SSLSession arg1) {return true;
}
});
InputStream inStream=con.getInputStream();
ByteArrayOutputStream outStream= newByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer,0, len);
}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) {returnaddressComponent;
}
}
}
}
}
}catch(Exception e) {
e.printStackTrace();
}return null;
}public static voidmain(String[] args) {
System.out.println(getLatAndLng("39.984154", "116.307490", "OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"));
}
}