百度地图添加多个行政区划

添加多个行政区划 并给他添加不同颜色

示例图

下面是代码

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
<script type="text/javascript" src="http://api.map.baidu.com/getscript?v=1.4"></script>
    <title>添加多个行政区划</title>
</head>
<body>
    <div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(112.533643,32.998098), 9);
    
    map.enableScrollWheelZoom();
    madeBoundary();
    

//区域图
    function madeBoundary() {
        
    var datas = new Array("南召县-#665599","方城县-#559955","邓州市-#666ddd","社旗县-#1199cc");
        var bdary = new BMap.Boundary();
        for(var i=0;i<datas.length;i++){
        getBoundary(datas[i],bdary);
            
        } 
        
    } 
//设置区域图

    function getBoundary(data,bdary){    
      data = data.split("-");
        bdary.get(data[0], function(rs){       //获取行政区域
            var count = rs.boundaries.length; //行政区域的点有多少个

              var pointArray = [];
            for (var i = 0; i < count; i++) {
                var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000",fillOpacity:0.5,fillColor:data[1]}); //建立多边形覆盖物
                
                map.addOverlay(ply);  //添加覆盖物

            }    
            
    
                            
        }); 


        
    }


</script>

 

转载于:https://www.cnblogs.com/jianz/p/5215279.html

<think>我们面对的问题:在百度地图中动态隐藏和显示行政区划的Polygon。 根据引用[2]中的思想,我们需要为每个行政区划的Polygon指定一个唯一标识,然后通过这个标识来控制其显示和隐藏。 百度地图JavaScript API中,多边形(Polygon)是覆盖物(Overlay)的一种。要动态隐藏和显示,我们可以使用覆盖物的方法:show()和hide()。 步骤: 1. 创建行政区划的Polygon时,为其设置一个唯一标识(例如id属性)。 2. 将这些Polygon对象存储在一个对象或Map中,以id为键,方便后续查找。 3. 当需要隐藏某个行政区划时,通过id找到对应的Polygon对象,调用其hide()方法。 4. 当需要显示时,调用show()方法。 示例代码: 首先,在创建行政区划的Polygon时,我们为其添加一个唯一标识(id)并存储起来: ```javascript // 存储所有行政区划的Polygon,以id为键 var districtPolygons = {}; // 假设我们有一个行政区划数据数组,每个元素包含id和边界点 var districts = [ { id: 'district1', name: '朝阳区', path: [ /* 经纬度数组 */ ] }, // ... 其他行政区 ]; // 遍历创建Polygon并存储 districts.forEach(function(district) { var polygon = new BMap.Polygon(district.path, { strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5, fillColor: "#fff", fillOpacity: 0.5 }); // 为polygon对象添加一个id属性,用于标识 polygon.id = district.id; // 存储 districtPolygons[district.id] = polygon; // 添加到地图 map.addOverlay(polygon); }); ``` 然后,我们可以通过以下函数来隐藏或显示指定的行政区划: ```javascript // 隐藏指定id的行政区划 function hideDistrict(id) { if (districtPolygons[id]) { districtPolygons[id].hide(); } } // 显示指定id的行政区划 function showDistrict(id) { if (districtPolygons[id]) { districtPolygons[id].show(); } } ``` 另外,引用[2]中提到,地图厂家提供的接口没有唯一标识参数,所以我们需要自己管理唯一标识。这里我们通过给每个Polygon对象添加一个id属性,并维护一个字典(districtPolygons)来实现。 注意:百度地图的覆盖物(包括Polygon)确实没有直接设置唯一标识的属性,所以我们自定义一个属性(id)是可行的。 如果需要在点击某个按钮时隐藏某个行政区,可以这样调用: ```javascript hideDistrict('district1'); ``` 这样,我们就实现了动态隐藏和显示行政区划的Polygon。 相关问题: 1. 如何在百度地图中为覆盖物(如Polygon)添加自定义属性? 2. 除了hide和show方法,还有哪些方法可以控制覆盖物的显示状态? 3. 如何批量隐藏或显示多个行政区划?</think>在百度地图中动态隐藏和显示行政区划的Polygon,需要通过以下步骤实现: ### 解决方案 1. **创建行政区划Polygon时添加唯一标识** 为每个行政区划的Polygon对象设置自定义属性(如`id`),用于后续定位: ```javascript // 创建行政区划Polygon(以北京市朝阳区为例) const points = [/* 经纬度点数组 */]; const polygon = new BMap.Polygon(points, { strokeColor: "blue", fillColor: "transparent" }); polygon.id = "chaoyang"; // 关键:设置唯一标识 map.addOverlay(polygon); ``` 2. **存储Polygon对象** 使用对象或Map存储所有行政区划,以`id`为键: ```javascript const districtPolygons = {}; districtPolygons["chaoyang"] = polygon; ``` 3. **动态隐藏/显示控制** 通过唯一标识调用百度地图API的`hide()`/`show()`方法: ```javascript // 隐藏行政区划 function hideDistrict(id) { if (districtPolygons[id]) { districtPolygons[id].hide(); } } // 显示行政区划 function showDistrict(id) { if (districtPolygons[id]) { districtPolygons[id].show(); } } // 示例:隐藏朝阳区 hideDistrict("chaoyang"); ``` ### 关键说明 - **唯一标识必要性**:百度地图原生API不提供覆盖物的唯一标识参数,需自定义属性(如`id`)实现精准控制[^2]。 - **性能优化**:存储Polygon对象避免每次遍历所有覆盖物,提升操作效率。 - **交互扩展**:可结合按钮/复选框触发`hideDistrict()`/`showDistrict()`,实现交互式控制。 ### 完整示例 ```html <button onclick="hideDistrict('chaoyang')">隐藏朝阳区</button> <button onclick="showDistrict('chaoyang')">显示朝阳区</button> <script> // 初始化地图和行政区划 const map = new BMap.Map("container"); const districtPolygons = {}; function initDistricts() { // 添加朝阳区 const chaoyangPoints = [/* 经纬度数组 */]; const chaoyangPoly = new BMap.Polygon(chaoyangPoints, {strokeColor: "blue"}); chaoyangPoly.id = "chaoyang"; map.addOverlay(chaoyangPoly); districtPolygons["chaoyang"] = chaoyangPoly; // 添加其他区... } </script> ``` --- ### 相关问题 1. **如何为百度地图Polygon添加点击事件并显示区域信息?** > 通过`addEventListener`绑定点击事件,结合`InfoWindow`展示自定义信息[^1]。 2. **如何根据数据动态改变Polygon颜色(如健康值)?** > 修改Polygon的`setFillColor()`和`setStrokeColor()`方法,根据阈值设置颜色[^1]。 3. **如何实现多个行政区划的批量显示/隐藏?** > 扩展`districtPolygons`结构,通过循环遍历执行操作: > ```javascript > function toggleDistricts(ids, isVisible) { > ids.forEach(id => { > isVisible ? showDistrict(id) : hideDistrict(id); > }); > } > ``` 4. **行政区划数据如何获取?** > 使用百度地图`Boundary`类获取行政边界点: > ```javascript > const bdary = new BMap.Boundary(); > bdary.get("朝阳区", result => { > const points = result.boundaries[0].split(";").map(p => { > const [lng, lat] = p.split(","); > return new BMap.Point(lng, lat); > }); > // 创建Polygon... > }); > ``` [^1]: 引用自百度地图标注点案例,涉及覆盖物样式控制和信息展示。 [^2]: 引用自Qt/C++地图开发经验,强调唯一标识在动态操作中的必要性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值