高德地图添加遮罩,实现圈出某个特定的地区

14 篇文章 0 订阅

实现效果
在这里插入图片描述

一、准备

1、注册账号并申请Key
在这里插入图片描述

2、准备页面

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> 
 <div id="container"></div>
#container {width:300px; height: 180px; }  

二、页面渲染

    let map = new AMap.Map(id, {
          resizeEnable: true, //是否监控地图容器尺寸变化
          mapStyle: "amap://styles/darkblue",
          zoom: 13,
          center: [113.94168366406248, 22.542299426249087],
        });

三、在地图上添加标记点,并hover标记点的时候显示自定义的title

在这里插入图片描述
思路:自己在html写title的样式

   <div id="container"></div>
   <div class="map_title">
       {{schoolNamList[thisIndex]}}<br />学校总数:10<br />特别关注人数:10<br />心理行为问题学校占比10%
   </div>

lnglats需要标记点的经纬度数组: [[113.91765107128904, 22.543072335306334]]
thisIndex是自定义title需要用到的下标

       let marker
        for (let i = 0; i < that.lnglats.length; i++) {
          marker = new AMap.Marker({
            position: that.lnglats[i],
            map: map
          })
          // 自定义点标记内容
          let markerContent = document.createElement("div");
          // 点标记中的图标
          let markerImg = document.createElement("img");
          markerImg.className = "marker_img";
          markerImg.style = "width: 58px;height: 58px;position: relative;"
          markerImg.src = "https://pub-static.aokaoyun.com/image/2021-05-22/33.png";
          markerContent.appendChild(markerImg);
          marker.setContent(markerContent); //更新点标记内容
          //移入标记点,显示自定义的title
          (function (markerImg, i) {
            markerImg.onmouseover = (e) => {
              that.thisIndex = i
              let mapTitle = document.getElementsByClassName("map_title");
              mapTitle[0].style.display = "block";
              mapTitle[0].style.left = e.x + 20 + "px"
              mapTitle[0].style.top = e.y + "px"
            }
          })(markerImg, i);
          //移入标记点,隐藏自定义的title
          (function (markerImg) {
            markerImg.onmouseout = () => {
              let mapTitle = document.getElementsByClassName("map_title");
              mapTitle[0].style.display = "none";
            }
          })(markerImg);
        }

四、在地图上添加遮罩,实现圈出某个特定的地区

    	map.setFitView()
        AMap.plugin('AMap.DistrictSearch', function () {
          new AMap.DistrictSearch({
            extensions: 'all',
            subdistrict: 0
          }).search('深圳市', function (status, result) {
            // 外多边形坐标数组和内多边形坐标数组
            let outer = [
              new AMap.LngLat(-360, 90, true),
              new AMap.LngLat(-360, -90, true),
              new AMap.LngLat(360, -90, true),
              new AMap.LngLat(360, 90, true),
            ];
            let holes = result.districtList[0].boundaries
            let pathArray = [
              outer
            ];
            pathArray.push.apply(pathArray, holes)
            let polygon = new AMap.Polygon({
              pathL: pathArray,
              strokeColor: '#04082b',
              strokeWeight: 1,
              fillColor: '#155292',
              fillOpacity: 0.5
            });
            polygon.setPath(pathArray);
            map.add(polygon)
          })
        })

在这里插入图片描述

五、完整的js代码

        let that = this
        let map = new AMap.Map('container', {
            resizeEnable: true, //是否监控地图容器尺寸变化
            mapStyle: "amap://styles/darkblue",//地图的皮肤
            zoom: 13,//级别范围
            center: [113.94168366406248, 22.542299426249087],//地图中心点
        });
		//标记点
        let marker
        for (let i = 0; i < that.lnglats.length; i++) {
            marker = new AMap.Marker({
                position: that.lnglats[i],
                map: map
            })
            // 自定义点标记内容
            let markerContent = document.createElement("div");
            // 点标记中的图标
            let markerImg = document.createElement("img");
          	markerImg.className = "marker_img";
          	markerImg.style = "width: 58px;height: 58px;position: relative;"
          	markerImg.src = "https://pub-static.aokaoyun.com/image/2021-05-22/33.png";
            markerContent.appendChild(markerImg);
            marker.setContent(markerContent); //更新点标记内容
            (function (markerImg, i) {
                markerImg.onmouseover = (e) => {
                    that.thisIndex = i
                    let mapTitle = document.getElementsByClassName("map_title");
                    mapTitle[0].style.display = "block";
                    mapTitle[0].style.left = e.x + 20 + "px"
                    mapTitle[0].style.top = e.y + "px"
                }
            })(markerImg, i);
            (function (markerImg) {
                markerImg.onmouseout = () => {
                    let mapTitle = document.getElementsByClassName("map_title");
                    mapTitle[0].style.display = "none";
                }
            })(markerImg);


        }
		//添加遮罩
        map.setFitView()
        AMap.plugin('AMap.DistrictSearch', function () {
            new AMap.DistrictSearch({
                extensions: 'all',
                subdistrict: 0
            }).search('深圳市', function (status, result) {
                // 外多边形坐标数组和内多边形坐标数组
                let outer = [
                    new AMap.LngLat(-360, 90, true),
                    new AMap.LngLat(-360, -90, true),
                    new AMap.LngLat(360, -90, true),
                    new AMap.LngLat(360, 90, true),
                ];
                let holes = result.districtList[0].boundaries
                let pathArray = [
                    outer
                ];
                pathArray.push.apply(pathArray, holes)
                let polygon = new AMap.Polygon({
                    pathL: pathArray,
                    strokeColor: '#04082b',
                    strokeWeight: 1,
                    fillColor: '#155292',
                    fillOpacity: 0.5
                });
                polygon.setPath(pathArray);
                map.add(polygon)
            })
        })

六、去除logo和版本号

在这里插入图片描述

去掉logo

.amap-logo{
    display: none;
}

去掉版本号

.amap-copyright {
    opacity:0;
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

守望黑玫瑰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值