| | |
| | | console.log("市区选择变化,重新获取地图数据"); |
| | | getMapData(); |
| | | }); |
| | | document.getElementById("find").addEventListener("click", function () { |
| | | var level = document.getElementById("level").value; |
| | | var keyword = document.getElementById("district").value; |
| | | |
| | | if (!keyword) { |
| | | Feng.error("请输入要查询的行政区名称或编码"); |
| | | return; |
| | | } |
| | | |
| | | // 清除旧的边界 |
| | | if (currentBoundaryPolygon) { |
| | | map.removeOverlay(currentBoundaryPolygon); |
| | | currentBoundaryPolygon = null; |
| | | } |
| | | |
| | | var bdary = new BMap.Boundary(); |
| | | bdary.get(keyword, function (rs) { |
| | | if (rs && rs.boundaries && rs.boundaries.length > 0) { |
| | | for (var i = 0; i < rs.boundaries.length; i++) { |
| | | var polygon = new BMap.Polygon(rs.boundaries[i], { |
| | | strokeColor: "#0000FF", |
| | | strokeWeight: 2, |
| | | fillColor: "#1791fc", |
| | | fillOpacity: 0.4 |
| | | }); |
| | | map.addOverlay(polygon); |
| | | currentBoundaryPolygon = polygon; |
| | | |
| | | // 获取多边形中心点 |
| | | var path = polygon.getPath(); |
| | | var bounds = new BMap.Bounds(path[0], path[0]); |
| | | for (var j = 1; j < path.length; j++) { |
| | | bounds.extend(path[j]); |
| | | } |
| | | var centerPoint = bounds.getCenter(); |
| | | |
| | | // 手动设置中心 + 缩放级别 |
| | | map.setCenter(centerPoint); |
| | | |
| | | // 设置缩放级别: |
| | | if (level === "province") { |
| | | map.setZoom(8); // 省级视角 |
| | | } else if (level === "city") { |
| | | map.setZoom(10); // 市级视角 |
| | | } else if (level === "district") { |
| | | map.setZoom(12); // 区级视角 |
| | | } |
| | | } |
| | | } else { |
| | | Feng.error("未找到该行政区划边界"); |
| | | } |
| | | }); |
| | | }); |
| | | |
| | | }); |
| | | var currentBoundaryPolygon = null; // 存储当前高亮的区域边界 |
| | | |
| | | |
| | | // 初始化地图 |
| | | function initMap() { |