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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| </html> <!DOCTYPE html> <html>
<head> <meta charset="UTF-8"> <title>百度地图DEMO</title> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你申请的AK"></script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() {
var queryHouseOutline = function(hid, callback) { var baseURL = 'http://map.baidu.com/?reqflag=pcmap&coord_type=3&from=webmap&qt=ext&ext_ver=new&l=18'; var url = baseURL + "&uid=" + hid; callback && (window.queryHouseOutlineCallback = callback); $.ajax({ type: "get", async: false, url: url, dataType: "jsonp", jsonpCallback: "queryHouseOutlineCallback", success: function(datas) {} }); };
var queryHouse = function(house, city, ak, callback) { var baseURL = 'http://api.map.baidu.com/place/v2/search?output=json&scope=2'; var url = baseURL + "&q=" + house + "®ion=" + city + "&ak=" + ak; callback && (window.queryHouseCallback = callback); $.ajax({ type: "get", async: false, url: url, dataType: "jsonp", jsonpCallback: "queryHouseCallback", success: function(datas) {} }); };
var coordinateToPoints = function(map, coordinate) { var points = []; if (coordinate) { var arr = coordinate.split(";"); if (arr) { for (var i = 0; i < arr.length; i++) { var coord = arr[i].split(","); if (coord && coord.length == 2) { var mctXY = new BMap.Pixel(coord[0], coord[1]); var project = map.getMapType().getProjection(); var point = project.pointToLngLat(mctXY); points.push(new BMap.Point(point.lng, point.lat)); } } } } return points; };
var parseGeo = function(mocator) { if (typeof mocator != 'string') { return {}; } var t = mocator.split("|"); var n = parseInt(t[0]); var i = t[1]; var r = t[2]; var o = r.split(";"); if (n === 4) { for (var a = [], s = 0; s < o.length - 1; s++) { "1" === o[s].split("-")[0] && a.push(o[s].split("-")[1]); } o = a; o.push(""); } var u = []; switch (n) { case 1: u.push(o[0]); break; case 2: case 3: case 4: for (var s = 0; s < o.length - 1; s++) { var l = o[s]; if (l.length > 100) { l = l.replace(/(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*),(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*)(,)/g, "$1,$2;"); u.push(l); } else { for (var c = [], d = l.split(","), f = 0; f < d.length; f += 2) { var p = d[f]; var h = d[f + 1]; c.push(p + "," + h); } u.push(c.join(";")) } } break; default: break; }
if (u.length <= 1) { u = u.toString(); }
var result = { type: n, bound: i, points: u }; return result; };
var map = new BMap.Map("allmap"); map.centerAndZoom("北京", 19); map.addControl(new BMap.MapTypeControl());
map.enableScrollWheelZoom(false);
var showArea = function(city, area) { queryHouse(area, city, "你申请的AK", function(data) { if (data.message == 'ok') { var houses = data.results; if (houses && houses.length > 0) { var house = houses[0]; queryHouseOutline(house.uid, function(houseOutline) { var geo = houseOutline.content.geo; if (!geo) { var location = house.location; var point = new BMap.Point(location.lng, location.lat); map.centerAndZoom(point, 19); var marker = new BMap.Marker(point); marker.setAnimation(BMAP_ANIMATION_BOUNCE); map.addOverlay(marker); } else { map.clearOverlays(); var geoObj = parseGeo(geo); var points = coordinateToPoints(map, geoObj.points); var ply = new BMap.Polygon(points, { strokeWeight: 2, strokeColor: "#F01B2D", strokeOpacity: 0.9, fillColor: "transparent" }); map.addOverlay(ply); map.setViewport(ply.getPath()); } }); } } }); };
showArea($('#cityId').val(), $('#areaId').val());
$('#showBtn').click(function() { debugger; showArea($('#cityId').val(), $('#areaId').val()); });
$("#areaId").keydown(function(e) { if (event.keyCode == "13") { showArea($('#cityId').val(), $('#areaId').val()); } }) }); </script> </head>
<body> <table> <tr> <td>城市:</td> <td> <input id="cityId" type="text" value="北京" /> </td> <td>小区:</td> <td> <input id="areaId" type="text" value="故宫博物院" /> </td> <td> <button id="showBtn">显示</button> </td> </tr> </table> <div id="allmap" style="width: 90vw; height: 90vh;"></div> </body>
</html>
|