0%

利用百度地图得到行政区域轮廓线

1.利用百度地图得到行政区域轮廓线

利用到的API函数:Boundary()

代码实例:

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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>获取地区轮廓线</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3">
</script>
<style type="text/css">
body{font-size:13px;margin:10px}
#container{width:800px;height:500px;border:1px solid gray;margin:auto auto;}
#controler {text-align:center;margin-top:30px;}
</style>
</head>
<body>
<div id="container"></div>
<div id="controler">
输入省、直辖市或县名称:<input type="text" id="districtName" style="width:80px" value="合肥市">
<input type="button" onclick="getBoundary()" value="获取轮廓线"></div>

<script type="text/javascript">
var map = new BMap.Map("container");
map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
map.addControl(new BMap.NavigationControl({type: BMAP_NAVIGATION_CONTROL_SMALL}));
map.enableScrollWheelZoom();

function getBoundary(){
var bdary = new BMap.Boundary();
var name = document.getElementById("districtName").value;
bdary.get(name, function(rs){ //获取行政区域
map.clearOverlays(); //清除地图覆盖物
var count = rs.boundaries.length; //行政区域的点有多少个
for(var i = 0; i < count; i++){
var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
map.addOverlay(ply); //添加覆盖物
map.setViewport(ply.getPath()); //调整视野
}
});
}
</script>
</body>
</html>

显示效果为

2.接下来是根据坐标获取位置的行政区信息

API信息: http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad

1
http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=35.658651,139.745415&output=json&pois=1&ak=您的ak //GET请求
主要是更改location这个变量,这里有个问题,返回的数据格式不是严格的json格式字符串:
1
renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":116.33815299999995,"lat":39.94171507488761},"formatted_address":"北京市海淀区新苑街13号","business":"白石桥,车公庄,甘家口","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"北京市","city":"北京市","city_level":2,"district":"海淀区","town":"","adcode":"110108","street":"新苑街","street_number":"13号","direction":"附近","distance":"16"},"pois":[{"addr":"北京市海淀区三里河路1号11号楼(西苑饭店院内)","cp":" ","direction":"东南","distance":"129","name":"中国外贸金融租赁","poiType":"金融","point":{"x":116.3375433561377,"y":39.94247976148231},"tag":"金融;投资理财","tel":"","uid":"7c6d3881a8f4e35c1d5800e7","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"三里河路5号","cp":" ","direction":"西北","distance":"105","name":"五矿发展大厦-C座","poiType":"房地产","point":{"x":116.33879200079658,"y":39.94117976632641},"tag":"房地产;写字楼","tel":"","uid":"db101a6a677f6af9c7f02567","zip":"","parent_poi":{"name":"五矿发展大厦","tag":"房地产;写字楼","addr":"北京市海淀区三里河路7号","point":{"x":116.33929505188216,"y":39.94086859358035},"direction":"西北","distance":"176","uid":"d97e2bb301449ac60b1cc703"}},{"addr":"北京市海淀区三里河路1号","cp":" ","direction":"南","distance":"225","name":"西苑饭店","poiType":"酒店","point":{"x":116.33881894996186,"y":39.943185067585059},"tag":"酒店;五星级","tel":"","uid":"eb3675036510d10201ec0197","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"北京市海淀区三里河5号","cp":" ","direction":"西","distance":"126","name":"五矿发展大厦-B座","poiType":"房地产","point":{"x":116.33926810271686,"y":39.941553171738828},"tag":"房地产;写字楼","tel":"","uid":"a12baa40e1bdd956e956faae","zip":"","parent_poi":{"name":"五矿发展大厦","tag":"房地产;写字楼","addr":"北京市海淀区三里河路7号","point":{"x":116.33929505188216,"y":39.94086859358035},"direction":"西北","distance":"176","uid":"d97e2bb301449ac60b1cc703"}},{"addr":"北京市海淀区三里河路7号","cp":" ","direction":"西北","distance":"176","name":"五矿发展大厦","poiType":"房地产","point":{"x":116.33929505188216,"y":39.94086859358035},"tag":"房地产;写字楼","tel":"","uid":"d97e2bb301449ac60b1cc703","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"北京市海淀区西苑饭店南门","cp":" ","direction":"东南","distance":"119","name":"新苑街-15号院","poiType":"房地产","point":{"x":116.3375164069724,"y":39.94238295419036},"tag":"房地产;住宅区","tel":"","uid":"da5b62ec876c0e3b725e8089","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"海淀区三里河路甲1号西苑饭店副楼(动物园地铁站向西300米)","cp":" ","direction":"南","distance":"196","name":"养怡园食府","poiType":"美食","point":{"x":116.33805539027839,"y":39.943074431818278},"tag":"美食;中餐厅","tel":"","uid":"4ce10ce6608cf0b8441a321b","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"北京市海淀区三里河路5号","cp":" ","direction":"东北","distance":"214","name":"五色土幼儿园","poiType":"教育培训","point":{"x":116.3371301356031,"y":39.940460609374458},"tag":"教育培训;幼儿园","tel":"","uid":"d6ee87244bf557e0a8f0cd7d","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"三里河路7号","cp":" ","direction":"北","distance":"293","name":"北京国玉新疆和田玉文博馆","poiType":"购物","point":{"x":116.33848657692318,"y":39.93969995430771},"tag":"购物;商铺","tel":"","uid":"a662490a8097cd8adeee8b02","zip":"","parent_poi":{"name":"新疆大厦","tag":"酒店;其他","addr":"三里河路7号","point":{"x":116.33838776331707,"y":39.93911908469923},"direction":"北","distance":"376","uid":"50fb64fffbeb6f44e8f853ed"}},{"addr":"三里河路7号(北京西苑饭店南150米处)","cp":" ","direction":"北","distance":"314","name":"北京新疆大厦嘉宾楼","poiType":"酒店","point":{"x":116.33879200079658,"y":39.93959622795643},"tag":"酒店;其他","tel":"","uid":"dba22815e682addc4afd3ff8","zip":"","parent_poi":{"name":"新疆大厦","tag":"酒店;其他","addr":"三里河路7号","point":{"x":116.33838776331707,"y":39.93911908469923},"direction":"北","distance":"376","uid":"50fb64fffbeb6f44e8f853ed"}}],"roads":[],"poiRegions":[],"sematic_description":"中国外贸金融租赁东南129米","cityCode":131}})
因此需要正则表达式来进行提取:
1
r=re.findall(r"[(](.*)[)]$",response.text)##text里面为返回的字符串
同时,返回的数据是Unicode格式,json模块无法解析,因而需要先对其进行转码:
1
2
res=r[0].encode("utf-8")
rj=json.loads(res)
这里附加关于json模块的相关信息: 1. json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)   (1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)   (2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)

  1. json.dump()和json.load()主要用来读写json文件函数