地质所 沉降监测网建设项目
chenhuan
2024-05-16 0fdd42e318f51f9e3c6581473416af1cca69877f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.javaweb.platform.utils;
 
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.HttpEntity;
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 com.javaweb.common.json.JSON;
import com.javaweb.common.json.JSONObject;
 
 
public class TdtUtils {
    
    public static List<String> getGeocoding(String place) throws Exception {
        List<String> list=new ArrayList<>();
        String tk = "f70f2cd5a6df4e75a1ad26db7389f931";
        String queryStr = "http://api.tianditu.gov.cn/geocoder?ds={'keyWord':'" + place + "'}&tk=" + tk;
        queryStr = queryStr.replace("}", "%7D");
        queryStr = queryStr.replace("{", "%7B");
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(queryStr);
        CloseableHttpResponse response = client.execute(httpGet);
 
        HttpEntity entity = response.getEntity();
 
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
 
            JSONObject jsonObject = JSON.unmarshal(result, JSONObject.class);
 
            String location = jsonObject.get("location").toString();
 
            JSONObject jsonObjectLocation = JSON.unmarshal(location, JSONObject.class);
 
            String lat = jsonObjectLocation.get("lat").toString();
 
            String lon = jsonObjectLocation.get("lon").toString();
        
            list.add(lat);
            list.add(lon);
        
        }
        response.close();
        return list;
    }
}