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 getGeocoding(String place) throws Exception { List 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; } }