目录
1、注册账号登录
2、创建应用,获取key
3、查看接口示例
4、调用接口
4.1地理编码查询
4.2逆地理编码查询
天地图网址:天地图API
1、注册账号登录
2、创建应用,获取key
3、查看接口示例
4、调用接口
4.1地理编码查询
public class TiandituAPICallExample2 {
public static void main(String[] args) {
// 创建 RestTemplate 实例
RestTemplate restTemplate = new RestTemplate();
try {
// 构建 URL
String baseUrl = "http://api.tianditu.gov.cn/geocoder";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("ds", "{\"keyWord\":\"北京市朝阳区酒仙桥路甲10号\"}")
.queryParam("tk", "你自己的key");
String url = builder.toUriString();
// 发送 GET 请求并获取响应
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
String responseBody = response.getBody();
// 打印响应结果
System.out.println(responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果
封装获取地理坐标
public static Map<String,String> getCoordinate(String address) {
// 创建 RestTemplate 实例
RestTemplate restTemplate = new RestTemplate();
try {
// 构建 URL
String baseUrl = "http://api.tianditu.gov.cn/geocoder";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("ds", "{\"keyWord\":\""+address+"\"}")
.queryParam("tk", "你自己的key");
String url = builder.toUriString();
// 发送 GET 请求并获取响应
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
int statusCodeValue = response.getStatusCodeValue();
if (statusCodeValue == 200) { // 表示成功
String responseBody = response.getBody();
// 创建 JsonReader 对象
JsonReader jsonReader = Json.createReader(new StringReader(responseBody));
// 解析 JSON 响应为 JsonObject
JsonObject jsonObject = jsonReader.readObject();
// 关闭 JsonReader
jsonReader.close();
// 访问解析后的值
JsonObject locationObj = jsonObject.getJsonObject("location");
//坐标点显示经度
double lon = locationObj.getJsonNumber("lon").numberValue().doubleValue();
//坐标点显示纬度
double lat = locationObj.getJsonNumber("lat").numberValue().doubleValue();
Map map = new HashMap();
map.put("lon",lon);
map.put("lat",lat);
return map;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
4.2逆地理编码查询
通过坐标获取地理信息
public static String getAddress(Double lon, Double lat, String tk){
String postStr = "{'lon':"+lon+",'lat':"+lat+",'ver':1}";
String apiUrl = "http://api.tianditu.gov.cn/geocoder?postStr="+postStr+"&type=geocode&tk="+tk+"";
StringBuilder response = null;
try {
// 创建 URL 对象
URL url = new URL(apiUrl);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为 GET
connection.setRequestMethod("GET");
// 获取响应状态码
int responseCode = connection.getResponseCode();
System.out.println("Response code: " + responseCode);
// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 关闭连接
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
结果
所需依赖
<!--spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.29</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>