百度地图api区级以下⾏政区划
我们在使⽤百度地图api想要展⽰苏州市吴中区各乡镇的⾏政区范围;
地暖不热怎么办
但该功能⽬前只能展⽰区级及以上范围⾏政区划
-------------------------------------------------------------------------------------------------------------------------------------
研究了百度⾏政区划实现原理,它也是存储了⾏政区边界点集合,显⽰时通过点集合绘制多边形polygon,
那我们⾃⼰也可以按照这个逻辑来实现乡镇⾏政区划的展⽰,
那现在的问题就是如何来获取⾏政区边界的点集;
从百度地图api V1.1版本,polygon类提供了enableEditing()功能,我们可以在地图上拖动编辑边界点,
然后,再通过polygon类提供的getPath()⽅法返回边界点数组,
将我们⾃⼰编辑过的边界点数组存储起来,就实现了⾏政区划的数字话⼯作(当然,这个数字话⽐起专业的GIS平台还是有差距)
---------------------------------------------------------------------------------------------------------------------------------------
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap"); // 创建Map实例
map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
map.tCurrentCity("苏州"); // 设置地图显⽰的城市此项是必须设置的
map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT })); //向地图中添加⽐例尺控件
//------------------------ 显⽰镇级范围 ------------------
var polygons = [];
function getBoundary() {
//var bdary = new BMap.Boundary();
//("苏州市吴中区", function (rs) { //获取⾏政区域
// //map.clearOverlays(); //清除地图覆盖物
// var count = rs.boundaries.length; //⾏政区域的点有多少个
// if (count === 0) {
湖北崇阳// alert('未能获取当前输⼊⾏政区域');
// return;
// }
// var pointArray = [];低血糖怎么缓解
// for (var i = 0; i < count; i++) {
// var ply = new BMap.Polygon(rs.boundaries[i], { strokeWeight: 2, strokeColor: "#ff0000" }); //建⽴多边形覆盖物
// ableEditing(); //范围可编辑
// map.addOverlay(ply); //添加覆盖物
// pointArray = Path());
// }
// map.tViewport(pointArray); //调整视野
// addlabel();
//});
var areas = [];
var dongshan = "120.3651,31.019986;120.34469,31.038057;120.336067,31.043751;120.340091,31.051423;120.336929,31.058106;120.34814,31.05959;120.354177,31.066767;120.365675,31.072458;120.363663,31.081861;120.366537,31.08 var dongshan_sml = "120.311273,31.030693;120.308255,31.027723;120.301212,31.029579;120.298481,31.026609;120.296613,31.024257;120.2969,31.02141;120.292301,31.021657;120.274335,31.024133;120.278216,31.02797;120.285114, var linhu = "120.475807,31.102039;120.480837,31.112551;120.483568,31.117621;120.48709,31.120697;120.505262,31.121856;120.513886,31.122104;120.514461,31.117899;120.521648,31.116663;120.523372,31.123588;120.530271,31.126 var hengjing = "120.573025,31.130466;120.569576,31.117606;120.547729,31.095345;120.532494,31.090892;120.530482,31.095839;120.537668,31.106724;120.520133,31.11167;120.523363,31.123588;120.530289,31.126026;120.525582,31 var yuexi = "120.561523,31.243425;120.576471,31.223602;120.586819,31.225578;120.601839,31.228604;120.60033,31.238979;120.607013,31.237003;120.610103,31.233607;120.612116,31.234224;120.61614,31.236324;120.633819,31.236 var xukou = "120.46341,31.262179;120.459961,31.271316;120.465135,31.274032;120.476346,31.267612;120.491581,31.261685;120.507104,31.260697;120.513643,31.261994;120.514434,31.248596;120.508972,31.248473;120.511056,31.2 var mudu = "120.535356,31.21917;120.538446,31.228002;120.54283,31.234424;120.554544,31.233869;120.561514,31.243193;120.562664,31.252146;120.574019,31.256962;120.576031,31.260296;120.565961,31.269896;120.564811,31.283 var xiangshan = "120.401158,31.25376;120.400735,31.258946;120.415575,31.261415;120.418513,
31.259849;120.420714,31.258822;120.422043,31.258097;120.424981,31.256515;120.424981,31.256515;120.432095,31.25281;120.428933,31 var jinting = "120.33966,31.16783;120.339947,31.157448;120.343397,31.152998;120.349433,31.155964;120.354895,31.15547;120.361506,31.150278;120.364094,31.140882;120.366968,31.134453;120.35087,31.118379;120.343684,31.11912 var guangfu = "120.448067,31.306618;120.432113,31.306495;120.420327,31.311431;120.421477,31.332901;120.40078,31.333148;120.397331,31.335616;120.375484,31.331174;120.379796,31.325745;120.358524,31.324018;120.363986,31 var luzhi = "120.87071,31.317417;120.86223,31.311;120.858924,31.301374;120.855187,31.290882;120.850588,31.288167;120.843833,31.290759;120.838802,31.294956;120.814656,31.299522;120.809769,31.291253;120.805314,31.285698; areas.push(dongshan);
areas.push(dongshan_sml);
areas.push(linhu);
areas.push(hengjing);
areas.push(yuexi);
areas.push(xukou);
areas.push(mudu);
areas.push(xiangshan);
areas.push(jinting);
areas.push(guangfu);
areas.push(luzhi);
colors = ["#fff492", "#fff492", "#98d057", "#e19ee6", "#1ad3da", "#ffc11d", "#98e800", "#b95817", "#03a9f4", "#39d0a4", "#8bbb53"];
var pointArray = [];
for (var i = 0; i < areas.length; i++) {
var ply = new BMap.Polygon(areas[i], { strokeWeight: 2, strokeColor: "#ff0000" }); //建⽴多边形覆盖物
ply.tFillColor(colors[i]) //设置多边形的填充颜⾊
ply.tFillOpacity(0.35);
polygons.push(ply); //加⼊多边形数组,以之后获取多边形边界点集
//if (i == 3)
//{
// ableEditing(); //范围可编辑【编辑的时候开启】
//}
map.addOverlay(ply); //添加覆盖物
pointArray = Path());
}
map.tViewport(pointArray); //调整视野刘强东演讲
addlabel();
}
tTimeout(function () {
getBoundary();
}, 2000);
function addlabel() {
var pointArray = [
new BMap.Point(120.384935, 31.063241), //东⼭镇
new BMap.Point(120.484461, 31.140899), //临湖镇
new BMap.Point(120.528582, 31.159412), //横泾镇
new BMap.Point(120.588424, 31.216319), //越溪镇
new BMap.Point(120.486159, 31.241906), //胥⼝镇
new BMap.Point(120.500029, 31.281492), //⽊渎镇
new BMap.Point(120.403673, 31.240549), //⾹⼭镇
new BMap.Point(120.288866, 31.131733), //⾦庭镇
new BMap.Point(120.387959, 31.306318), //光福镇
new BMap.Point(120.818048, 31.251493) //甪直镇
];
var optsArray = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}];
var labelArray = [];
var contentArray = [
"东⼭镇", "临湖镇", "横泾镇", "越溪镇", "胥⼝镇", "⽊渎镇", "⾹⼭镇","⾦庭镇","光福镇", "甪直镇"];笔记本电脑fn键
for (var i = 0; i < pointArray.length; i++) {
optsArray[i].position = pointArray[i];
做完某事的英文labelArray[i] = new BMap.Label(contentArray[i], optsArray[i]);
labelArray[i].tStyle({
color: "red",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅⿊"打水光针的危害
});
map.addOverlay(labelArray[i]);
}
}
/
/获取编辑后的边界点集【编辑完后,可以通过 button 来触发这个⽅法】
function getpoints() {
for (var j = 0; j < polygons.length; j++) {
猫品种大全及图片var polyline =polygons[j];
var pts = Path();
var sss = "";
for (var i = 0; i < pts.length; i++) {
sss += pts[i].lng + "," + pts[i].lat + ";";
}
console.log(sss); //【将点集输出到控制台】
}
alert('已输出边界点集合!');
}
</script>
编辑时的效果:
将边界点集输出到控制台:
最后将边界点集合存储下来,或者像我这边⼀样,直接写在js初始化函数⾥。