使用Java开发高德地图API的行政区划查询功能简介
使用Java开发高德地图API的行政区划查询功能简介
引言:随着移动互联网的快速发展,地图应用成为人们生活中难以替代的一部分。高德地图作为国内领先的地图应用之一,提供了丰富的API接口,可以方便地获取各种地图相关的数据。而其中的行政区划查询功能,可以帮助我们快速获取到指定区域的详细信息。本文将介绍如何使用Java开发高德地图API的行政区划查询功能,并附上代码示例。
com.alibaba fastjson 1.2.75 org.apache.httpcomponents httpclient 4.5.12 登录后复制
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class DistrictQuery { public static void main(String[] args) throws IOException { String key = "your-api-key"; String keywords = "上海市"; String url = "https://restapi.amap.com/v3/config/district?keywords=" + keywords + "&key=" + key; HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); JSONObject jsonObject = JSONObject.parseObject(result); JSONArray districts = jsonObject.getJSONArray("districts"); for (int i = 0; i < districts.size(); i++) { JSONObject district = districts.getJSONObject(i); String name = district.getString("name"); String level = district.getString("level"); String center = district.getString("center"); System.out.println("名称:" + name); System.out.println("级别:" + level); System.out.println("中心点坐标:" + center); } } }登录后复制
运行代码后,可以看到以下输出结果:
名称:上海市 级别:province 中心点坐标:121.4737,31.2304 名称:上海市 级别:city 中心点坐标:121.4737,31.2304 名称:黄浦区 级别:district 中心点坐标:121.4846,31.2316 ...登录后复制