获得多边形电子围栏
作者:互联网
<!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: 1500px;
height: 500px;
border: 1px solid gray
}
</style>
</head>
<body>
<div id="container"></div>
<br/>
<input type="text" id="districtName" style="width:1000px;height: 20px"
value="93.435871 38.794708,110.504895 37.515873,96.969735 32.683575,89.903578 36.216606,93.435871 38.794708">
<input type="button" onclick="getBoundary()" value="获取轮廓线">
<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 setting = document.getElementById("districtName").value;
if (!setting) {
alert("请输入setting")
}
map.clearOverlays(); //清除地图覆盖物
// var setting="113.326627 34.412251,113.320514 34.398594,113.323021 34.387795,113.31797 34.375389,113.31969 34.36063,113.306571 34.356678,113.288653 34.357841,113.280651 34.355199,113.274851 34.344771,113.259238 34.341825,113.240926 34.344752,113.23178 34.341167,113.227185 34.335448,113.22366 34.327688,113.211987 34.317663,113.200613 34.318375,113.191414 34.309428,113.187101 34.302755,113.187739 34.298533,113.182611 34.294166,113.179064 34.292459,113.174382 34.287596,113.169675 34.284719,113.168005 34.279358,113.155712 34.277248,113.13958 34.280813,113.111491 34.27163,113.091921 34.273948,113.082105 34.283559,113.074329 34.288694,113.07241 34.294662,113.066755 34.291017,113.050474 34.288896,113.0389 34.29632,113.033855 34.295265,113.02681 34.299355,113.020014 34.307157,113.00642 34.30254,112.990943 34.302692,112.970917 34.304524,112.960626 34.306654,112.949563 34.303673,112.939359 34.301909,112.928518 34.302133,112.917965 34.301066,112.906848 34.303851,112.893315 34.306605,112.882321 34.303427,112.873302 34.304643,112.858641 34.306187,112.843756 34.304309,112.84471 34.312795,112.848374 34.317496,112.857722 34.321154,112.8536 34.325308,112.845411 34.332294,112.837909 34.337793,112.828009 34.339859,112.8186 34.344594,112.806722 34.340855,112.793603 34.338034,112.791297 34.346559,112.786866 34.35113,112.778057 34.351377,112.767639 34.351107,112.763747 34.36368,112.740987 34.360241,112.734527 34.375622,112.726919 34.389568,112.729687 34.39648,112.736849 34.399908,112.737422 34.409042,112.734342 34.418753,112.739037 34.428796,112.746431 34.44572,112.744416 34.458785,112.748379 34.471216,112.747142 34.486981,112.745129 34.4973,112.770814 34.499302,112.784286 34.504028,112.804643 34.493037,112.818862 34.483417,112.839174 34.481722,112.866176 34.481433,112.885246 34.478939,112.910779 34.475205,112.92456 34.475666,112.931535 34.485946,112.938422 34.510355,112.946505 34.530522,112.973333 34.530986,112.995998 34.53707,112.987801 34.54914,112.978082 34.550674,112.967573 34.555376,112.949502 34.557628,112.935649 34.558331,112.920041 34.555808,112.903736 34.558298,112.894751 34.56326,112.885902 34.572307,112.879823 34.583403,112.870418 34.590552,112.8643 34.599408,112.85314 34.602965,112.847555 34.621484,112.849453 34.643508,112.84394 34.659421,112.838241 34.676697,112.836745 34.700822,112.85534 34.705993,112.877714 34.713559,112.896438 34.704132,112.909363 34.700492,112.919273 34.711038,112.92712 34.719127,112.937993 34.719012,112.935373 34.726647,112.917492 34.744493,112.913156 34.761338,112.917599 34.770557,112.912463 34.780999,112.896789 34.783982,112.877113 34.78297,112.857336 34.781872,112.839615 34.783085,112.823449 34.782192,112.822107 34.803402,112.815924 34.817669,112.818779 34.83303,112.834185 34.824771,112.848113 34.815034,112.859858 34.816554,112.869919 34.827135,112.87611 34.837979,112.892564 34.849306,112.917638 34.838696,112.943776 34.835554,112.972596 34.849311,112.994215 34.860643,113.027175 34.864741,113.058435 34.862664,113.076413 34.868228,113.101413 34.876414,113.113411 34.865321,113.124944 34.863796,113.134652 34.86192,113.151868 34.864053,113.146205 34.8864,113.151954 34.893795,113.173161 34.89905,113.191017 34.904438,113.219869 34.913042,113.231697 34.910862,113.241329 34.903799,113.253574 34.914616,113.274709 34.934085,113.305069 34.94688,113.332548 34.954539,113.354291 34.962776,113.384393 34.978137,113.409923 34.977918,113.434595 34.972351,113.473447 34.965217,113.502991 34.966948,113.522186 34.969234,113.546365 34.968027,113.56992 34.957531,113.606379 34.943922,113.644025 34.929786,113.679705 34.917693,113.749539 34.921186,113.786816 34.909021,113.813132 34.890149,113.849943 34.887601,113.867371 34.899083,113.883258 34.900168,113.913229 34.902558,113.94436 34.904696,113.97829 34.930235,114.002066 34.950418,114.029765 34.967747,114.053577 34.978136,114.076772 34.983181,114.118769 34.980788,114.143855 34.963401,114.172925 34.950814,114.21482 34.939721,114.243227 34.915738,114.23023 34.910794,114.221876 34.894851,114.214718 34.876955,114.213101 34.85907,114.21311 34.845399,114.204635 34.835774,114.192504 34.821439,114.185204 34.822717,114.163339 34.831897,114.1344 34.827454,114.136263 34.808828,114.160657 34.789651,114.177266 34.771046,114.163149 34.763663,114.152975 34.749392,114.144276 34.736622,114.149235 34.714166,114.157668 34.70905,114.1709 34.719971,114.165593 34.702187,114.16528 34.680761,114.162056 34.665737,114.159018 34.652962,114.160785 34.637686,114.156848 34.624232,114.16128 34.613129,114.152127 34.614855,114.143062 34.620742,114.132764 34.602736,114.119155 34.579294,114.111073 34.557985,114.104388 34.534933,114.098522 34.520333,114.093704 34.497994,114.083196 34.490207,114.064957 34.500747,114.039996 34.513762,114.025103 34.50121,114.009203 34.485828,114.017176 34.465184,114.001616 34.448578,113.982471 34.450362,113.952008 34.449709,113.949097 34.460274,113.932274 34.459181,113.909108 34.468015,113.902337 34.464899,113.906372 34.459863,113.895979 34.437289,113.884574 34.411919,113.892446 34.396129,113.89354 34.372794,113.894474 34.362937,113.893626 34.344515,113.895124 34.337013,113.883343 34.336792,113.868199 34.334009,113.857796 34.333488,113.852681 34.329823,113.836173 34.330589,113.82544 34.33571,113.812194 34.342814,113.803244 34.33878,113.798074 34.328643,113.772719 34.320519,113.75785 34.322398,113.743601 34.324823,113.737515 34.316744,113.731399 34.313135,113.722487 34.30925,113.717217 34.298643,113.707254 34.293236,113.709121 34.285539,113.703301 34.282968,113.693188 34.28894,113.681262 34.287515,113.667878 34.28263,113.656696 34.287796,113.652354 34.292102,113.639946 34.29934,113.630555 34.311135,113.603178 34.323896,113.57811 34.326624,113.562169 34.320579,113.541986 34.325795,113.521813 34.344655,113.516963 34.349091,113.509069 34.345314,113.499794 34.34823,113.489069 34.347246,113.484029 34.340494,113.481535 34.338437,113.470995 34.341699,113.46269 34.34425,113.457566 34.34389,113.453622 34.345558,113.445889 34.352752,113.437309 34.354589,113.430142 34.352332,113.420651 34.35564,113.416128 34.361401,113.416107 34.364444,113.421248 34.378117,113.413042 34.384736,113.402525 34.383103,113.396566 34.382239,113.390331 34.386997,113.3864 34.383639,113.378303 34.384931,113.375687 34.389576,113.378761 34.395586,113.364012 34.402492,113.360642 34.405513,113.349578 34.412123,113.34153 34.410157";
var finalSetting = setting.myReplace(",", ";").myReplace(" ", ", ");
console.log(finalSetting);
//"113.326627, 34.412251;113.320514, 34.398594;113.323021, 34.387795;113.31797, 34.375389;113.31969, 34.36063;113.306571, 34.356678;113.288653, 34.357841;113.280651, 34.355199;113.274851, 34.344771;113.259238, 34.341825;113.240926, 34.344752;113.23178, 34.341167;113.227185, 34.335448;113.22366, 34.327688;113.211987, 34.317663;113.200613, 34.318375;113.191414, 34.309428;113.187101, 34.302755;113.187739, 34.298533;113.182611, 34.294166;113.179064, 34.292459;113.174382, 34.287596;113.169675, 34.284719;113.168005, 34.279358;113.155712, 34.277248;113.13958, 34.280813;113.111491, 34.27163;113.091921, 34.273948;113.082105, 34.283559;113.074329, 34.288694;113.07241, 34.294662;113.066755, 34.291017;113.050474, 34.288896;113.0389, 34.29632;113.033855, 34.295265;113.02681, 34.299355;113.020014, 34.307157;113.00642, 34.30254;112.990943, 34.302692;112.970917, 34.304524;112.960626, 34.306654;112.949563, 34.303673;112.939359, 34.301909;112.928518, 34.302133;112.917965, 34.301066;112.906848, 34.303851;112.893315, 34.306605;112.882321, 34.303427;112.873302, 34.304643;112.858641, 34.306187;112.843756, 34.304309;112.84471, 34.312795;112.848374, 34.317496;112.857722, 34.321154;112.8536, 34.325308;112.845411, 34.332294;112.837909, 34.337793;112.828009, 34.339859;112.8186, 34.344594;112.806722, 34.340855;112.793603, 34.338034;112.791297, 34.346559;112.786866, 34.35113;112.778057, 34.351377;112.767639, 34.351107;112.763747, 34.36368;112.740987, 34.360241;112.734527, 34.375622;112.726919, 34.389568;112.729687, 34.39648;112.736849, 34.399908;112.737422, 34.409042;112.734342, 34.418753;112.739037, 34.428796;112.746431, 34.44572;112.744416, 34.458785;112.748379, 34.471216;112.747142, 34.486981;112.745129, 34.4973;112.770814, 34.499302;112.784286, 34.504028;112.804643, 34.493037;112.818862, 34.483417;112.839174, 34.481722;112.866176, 34.481433;112.885246, 34.478939;112.910779, 34.475205;112.92456, 34.475666;112.931535, 34.485946;112.938422, 34.510355;112.946505, 34.530522;112.973333, 34.530986;112.995998, 34.53707;112.987801, 34.54914;112.978082, 34.550674;112.967573, 34.555376;112.949502, 34.557628;112.935649, 34.558331;112.920041, 34.555808;112.903736, 34.558298;112.894751, 34.56326;112.885902, 34.572307;112.879823, 34.583403;112.870418, 34.590552;112.8643, 34.599408;112.85314, 34.602965;112.847555, 34.621484;112.849453, 34.643508;112.84394, 34.659421;112.838241, 34.676697;112.836745, 34.700822;112.85534, 34.705993;112.877714, 34.713559;112.896438, 34.704132;112.909363, 34.700492;112.919273, 34.711038;112.92712, 34.719127;112.937993, 34.719012;112.935373, 34.726647;112.917492, 34.744493;112.913156, 34.761338;112.917599, 34.770557;112.912463, 34.780999;112.896789, 34.783982;112.877113, 34.78297;112.857336, 34.781872;112.839615, 34.783085;112.823449, 34.782192;112.822107, 34.803402;112.815924, 34.817669;112.818779, 34.83303;112.834185, 34.824771;112.848113, 34.815034;112.859858, 34.816554;112.869919, 34.827135;112.87611, 34.837979;112.892564, 34.849306;112.917638, 34.838696;112.943776, 34.835554;112.972596, 34.849311;112.994215, 34.860643;113.027175, 34.864741;113.058435, 34.862664;113.076413, 34.868228;113.101413, 34.876414;113.113411, 34.865321;113.124944, 34.863796;113.134652, 34.86192;113.151868, 34.864053;113.146205, 34.8864;113.151954, 34.893795;113.173161, 34.89905;113.191017, 34.904438;113.219869, 34.913042;113.231697, 34.910862;113.241329, 34.903799;113.253574, 34.914616;113.274709, 34.934085;113.305069, 34.94688;113.332548, 34.954539;113.354291, 34.962776;113.384393, 34.978137;113.409923, 34.977918;113.434595, 34.972351;113.473447, 34.965217;113.502991, 34.966948;113.522186, 34.969234;113.546365, 34.968027;113.56992, 34.957531;113.606379, 34.943922;113.644025, 34.929786;113.679705, 34.917693;113.749539, 34.921186;113.786816, 34.909021;113.813132, 34.890149;113.849943, 34.887601;113.867371, 34.899083;113.883258, 34.900168;113.913229, 34.902558;113.94436, 34.904696;113.97829, 34.930235;114.002066, 34.950418;114.029765, 34.967747;114.053577, 34.978136;114.076772, 34.983181;114.118769, 34.980788;114.143855, 34.963401;114.172925, 34.950814;114.21482, 34.939721;114.243227, 34.915738;114.23023, 34.910794;114.221876, 34.894851;114.214718, 34.876955;114.213101, 34.85907;114.21311, 34.845399;114.204635, 34.835774;114.192504, 34.821439;114.185204, 34.822717;114.163339, 34.831897;114.1344, 34.827454;114.136263, 34.808828;114.160657, 34.789651;114.177266, 34.771046;114.163149, 34.763663;114.152975, 34.749392;114.144276, 34.736622;114.149235, 34.714166;114.157668, 34.70905;114.1709, 34.719971;114.165593, 34.702187;114.16528, 34.680761;114.162056, 34.665737;114.159018, 34.652962;114.160785, 34.637686;114.156848, 34.624232;114.16128, 34.613129;114.152127, 34.614855;114.143062, 34.620742;114.132764, 34.602736;114.119155, 34.579294;114.111073, 34.557985;114.104388, 34.534933;114.098522, 34.520333;114.093704, 34.497994;114.083196, 34.490207;114.064957, 34.500747;114.039996, 34.513762;114.025103, 34.50121;114.009203, 34.485828;114.017176, 34.465184;114.001616, 34.448578;113.982471, 34.450362;113.952008, 34.449709;113.949097, 34.460274;113.932274, 34.459181;113.909108, 34.468015;113.902337, 34.464899;113.906372, 34.459863;113.895979, 34.437289;113.884574, 34.411919;113.892446, 34.396129;113.89354, 34.372794;113.894474, 34.362937;113.893626, 34.344515;113.895124, 34.337013;113.883343, 34.336792;113.868199, 34.334009;113.857796, 34.333488;113.852681, 34.329823;113.836173, 34.330589;113.82544, 34.33571;113.812194, 34.342814;113.803244, 34.33878;113.798074, 34.328643;113.772719, 34.320519;113.75785, 34.322398;113.743601, 34.324823;113.737515, 34.316744;113.731399, 34.313135;113.722487, 34.30925;113.717217, 34.298643;113.707254, 34.293236;113.709121, 34.285539;113.703301, 34.282968;113.693188, 34.28894;113.681262, 34.287515;113.667878, 34.28263;113.656696, 34.287796;113.652354, 34.292102;113.639946, 34.29934;113.630555, 34.311135;113.603178, 34.323896;113.57811, 34.326624;113.562169, 34.320579;113.541986, 34.325795;113.521813, 34.344655;113.516963, 34.349091;113.509069, 34.345314;113.499794, 34.34823;113.489069, 34.347246;113.484029, 34.340494;113.481535, 34.338437;113.470995, 34.341699;113.46269, 34.34425;113.457566, 34.34389;113.453622, 34.345558;113.445889, 34.352752;113.437309, 34.354589;113.430142, 34.352332;113.420651, 34.35564;113.416128, 34.361401;113.416107, 34.364444;113.421248, 34.378117;113.413042, 34.384736;113.402525, 34.383103;113.396566, 34.382239;113.390331, 34.386997;113.3864, 34.383639;113.378303, 34.384931;113.375687, 34.389576;113.378761, 34.395586;113.364012, 34.402492;113.360642, 34.405513;113.349578, 34.412123;113.34153, 34.410157";
var ply = new BMap.Polygon(finalSetting, {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
map.addOverlay(ply); //添加覆盖物
map.setViewport(ply.getPath()); //调整视野
}
String.prototype.myReplace = function (f, e) {//吧f替换成e
var reg = new RegExp(f, "g"); //创建正则RegExp对象
return this.replace(reg, e);
}
</script>
</body>
</html>
标签:map,多边形,电子,finalSetting,围栏,setting,BMap,var,new 来源: https://blog.csdn.net/qq_42424503/article/details/111668519