本文提供两种方案,请根据自己的需求获取数据
方案一:高德地图接口
数据来源
https://lbs.amap.com/api/webservice/guide/api/district.
key申请
https://console.amap.com/dev/key/app.
接口地址
请根据自己的需求访问接口
https://restapi.amap.com/v3/config/district?keywords=xxx&subdistrict=3&key=你申请的key
部分代码
package cn.zifangsky.task;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class Gaode {
private static int id = 1;
public static void main(String[] args) {
String url = "https://restapi.amap.com/v3/config/district?keywords=中国&subdistrict=1&key=xxx";
String urlResult = HttpUtil.get(url);
StringBuffer sb = new StringBuffer();
JSONObject resultObj = JSONObject.parseObject(urlResult);
if("1".equals(resultObj.getString("status"))){
JSONArray countrys = resultObj.getJSONArray("districts");
sb.append("INSERT INTO city_area(id,pid,name,merger_name,level,pinyin,ad_code,city_code,lng,lat) values\n");
if(countrys.size()==1){
JSONObject country = countrys.getJSONObject(0);
String c = "中国";
JSONArray provinces = country.getJSONArray("districts");
for(int i=0;i<provinces.size();i++){
JSONObject province = provinces.getJSONObject(i);
String adcode = province.getString("adcode");
String[] centers = province.getString("center").split(",");
String name = province.getString("name");
String level = province.getString("level");
String proFullName = c+","+name;
sb.append("("+id+",").append("0,").append("'"+name+"',").append("'"+proFullName+"',")
.append("'"+level+"',").append("'"+getPinyin(name)+"',").append("'"+adcode+"',").append("'',").append(centers[0]+",").append(centers[1]+"),\n");
id++;
}
}
System.out.println(sb.toString);
}
/**
* @param china (字符串 汉字)
* @return 汉字转拼音 其它字符不变
*/
public static String getPinyin(String china){
HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
formart.setCaseType(HanyuPinyinCaseType.LOWERCASE);
formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
formart.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] arrays = china.trim().toCharArray();
String result = "";
try {
for (int i=0;i<arrays.length;i++) {
char ti = arrays[i];
if(Character.toString(ti).matches("[\\u4e00-\\u9fa5]")){ //匹配是否是中文
String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti,formart);
result += temp[0];
}else{
result += ti;
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return result;
}
}
资源下载
下载内容为mysql脚本,部分展示如下所示:
mysql脚本下载.
上面资源包括java可执行代码,可取到区县数据。
DROP TABLE IF EXISTS `city_area`;
create table city_area
(
id int(4) not null comment 'id',
pid int(4) null,
name varchar(16) null comment '名称',
merger_name varchar(32) null comment '全名',
level varchar(12) null comment '层级 省province 市city 区县district',
pinyin varchar(64) null,
ad_code varchar(16) null comment '区域行政编码',
city_code varchar(8) null comment '城市编码',
lng double null comment '经度',
lat double null comment '纬度',
constraint city_area_id_uindex
unique (id)
);
alter table city_area add primary key (id);
INSERT INTO city_area(id,pid,name,merger_name,level,pinyin,ad_code,city_code,lng,lat) values
(1,0,'广东省','中国,广东省','province','guangdongsheng','440000','',113.280637,23.125178),
(2,1,'汕头市','中国,广东省,汕头市','city','shantoushi','440500','0754',116.708463,23.125178),
(3,2,'潮阳区','中国,广东省,汕头市,潮阳区','district','chaoyangqu','440513','0754',116.602602,23.262336),
(4,2,'澄海区','中国,广东省,汕头市,澄海区','district','chenghaiqu','440515','0754',116.76336,23.46844),
(5,2,'南澳县','中国,广东省,汕头市,南澳县','district','nanaoxian','440523','0754',117.027105,23.419562),
(6,2,'潮南区','中国,广东省,汕头市,潮南区','district','chaonanqu','440514','0754',116.423607,23.249798),
(7,2,'濠江区','中国,广东省,汕头市,濠江区','district','haojiangqu','440512','0754',116.729528,23.279345),
(8,2,'金平区','中国,广东省,汕头市,金平区','district','jinpingqu','440511','0754',116.703583,23.367071),
(9,2,'龙湖区','中国,广东省,汕头市,龙湖区','district','longhuqu','440507','0754',116.732015,23.373754),
(10,1,'佛山市','中国,广东省,佛山市','city','foshanshi','440600','0757',113.122717,23.125178),
(11,10,'三水区','中国,广东省,佛山市,三水区','district','sanshuiqu','440607','0757',112.899414,23.16504),
(12,10,'高明区','中国,广东省,佛山市,高明区','district','gaomingqu','440608','0757',112.882123,22.893855),
(13,10,'顺德区','中国,广东省,佛山市,顺德区','district','shundequ','440606','0757',113.281826,22.75851),
(14,10,'禅城区','中国,广东省,佛山市,禅城区','district','shanchengqu','440604','0757',113.112414,23.019643),
(15,10,'南海区','中国,广东省,佛山市,南海区','district','nanhaiqu','440605','0757',113.145577,23.031562),
(16,1,'肇庆市','中国,广东省,肇庆市','city','zhaoqingshi','441200','0758',112.472529,23.125178),
(17,16,'封开县','中国,广东省,肇庆市,封开县','district','fengkaixian','441225','0758',111.502973,23.434731),
(18,16,'怀集县','中国,广东省,肇庆市,怀集县','district','huaijixian','441224','0758',112.182466,23.913072),
(19,16,'广宁县','中国,广东省,肇庆市,广宁县','district','guangningxian','441223','0758',112.440419,23.631486),
(20,16,'德庆县','中国,广东省,肇庆市,德庆县','district','deqingxian','441226','0758',111.78156,23.141711),
(21,16,'四会市','中国,广东省,肇庆市,四会市','district','sihuishi','441284','0758',112.695028,23.340324),
(22,16,'鼎湖区','中国,广东省,肇庆市,鼎湖区','district','dinghuqu','441203','0758',112.565249,23.155822),
(23,16,'端州区','中国,广东省,肇庆市,端州区','district','duanzhouqu','441202','0758',112.472329,23.052662),
(24,16,'高要区','中国,广东省,肇庆市,高要区','district','gaoyaoqu','441204','0758',112.460846,23.027694),
(25,1,'惠州市','中国,广东省,惠州市','city','huizhoushi','441300','0752',114.412599,23.125178),
(26,25,'龙门县','中国,广东省,惠州市,龙门县','district','longmenxian','441324','0752',114.259986,23.723894),
(27,25,'博罗县','中国,广东省,惠州市,博罗县','district','boluoxian','441322','0752',114.284254,23.167575),
(28,25,'惠东县','中国,广东省,惠州市,惠东县','district','huidongxian','441323','0752',114.723092,22.983036)
数据库表内容:
方案二: 城市编码开放数据
数据来源
城市编码开放数据查询: http://citycode.blacklife.cn/index.php.
本文暂不提供爬取数据的代码。