事情是这样的,最近公司有一个业务需求,需要根据经纬度计算出具体的位置信息。于是打算用高德地图的Api来实现。也是很简单的,就调用一个http的api就返回了结果。(感叹第三方的服务的强大啊)如下是一个简单的测试代码:
@Test
public void testGaoDe() {
String lng = "106.532353";
String lat = "29.572062";
String url = "https://restapi.amap.com/v3/geocode/regeo?"
+ "key=bcbddf874b42153b9fc3aadb1f775809&output=JSON&location="
+ lng + "," + lat;
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
InputStream is = conn.getInputStream();
//读取输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String readLine;
while ( (readLine = reader.readLine()) != null ){
System.out.println(readLine);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
运行可以看到返回了一个json数据,这样我就可以做我想做的了!到这里一切都很完美。
{
"status":"1","regeocode":{
"addressComponent":{
"city":[],"province":"重庆市","adcode":"500105","district":"江北区","towncode":"500105006000","streetNumber":{
"number":"1-4号","location":"106.532424,29.572047","direction":"东","distance":"7.05076","street":"建新西路"},"country":"中国","township":"观音桥街道","businessAreas":[{
"location":"106.533939,29.576787","name":"观音桥","id":"500105"},{
"location":"106.537491,29.568066","name":"华新街","id":"500105"},{
"location":"106.533912,29.575701","name":"江北(商圈)","id":"500105"}],"building":