vue高德地图添加标记点,点击Marker图标放大其余保持原状

1 篇文章 0 订阅

需求:地图添加编辑点,被点击的点放大,其余点不变。

官网教程:JSAPI结合Vue使用-JSAPI与前端框架结合-教程-地图 JS API v2.0 | 高德地图API

 别忘记了设置地图安全密钥!

一、 地图初始化函数 initMap

methods:{
    initMap(){
        AMapLoader.load({
            key:"",             // 申请好的Web端开发者Key,首次调用 load 时必填
            version:"1.4.15",      // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
            plugins:[''],       // 需要使用的的插件列表,如比例尺'AMap.Scale'等
        }).then((AMap)=>{
            this.map = new AMap.Map("container",{  //设置地图容器id
               zoom: 16,
               mapStyle: "amap://styles/27531f8203a219e2fbc8838256a0ac28",
             
            });
        }).catch(e=>{
            console.log(e);
        })
    },
},

二、获取标记点列表,添加标记点

   mapList(obj)
        .then((res) => {
          this.markerList = [];// 用来装标记点
          res.forEach((element, index) => {
            //后端返回-标记点的经纬度
            let lng = Number(element.longitude);
            let lat = Number(element.latitude);

            // 图标地址
            let src=require(`@/assets/icon_dt_dw0.png`);
            
            // 配置marker
            let marker = new AMap.Marker({
              position: [lng, lat],
              icon: new AMap.Icon({
                image: src,
                imageSize: new AMap.Size(15, 21),
              }),
              offset: new AMap.Pixel(-20, -62),//设置偏移量
              extData: {
                index,
              },
            });


            // 给marker添加点击事件
            marker.on("click", (e) => {
                //获取当前点击的标记值
              let index = e.target.getExtData().index;

               // 将所有标记点与当前编辑点匹配,相同则放大图标,
               // 不同则设置为初始值(将其余被放大的标记点复原)
              this.markerList.forEach((markeritem) => {
                let index2 = markeritem.getExtData().index;
                let icon = e.target.getIcon();

                if (index === index2) {
                    // 相同-放大
                  e.target.setIcon(
                    new AMap.Icon({
                      image: icon.w.image, // Icon的图像
                      imageSize: new AMap.Size(20, 28),
                    })
                  );
                } else {
                 // 不同-复原
                  markeritem.setIcon(
                    new AMap.Icon({
                      image: markeritem.getIcon().w.image, // Icon的图像
                      imageSize: new AMap.Size(15, 21),
                    })
                  );
                }
              });     
              
            });

            this.markerList.push(marker);
          });
           // 将标记点数组放入Map
          this.Map.add(this.markerList);
     
        })
        .catch((e) => {
           console.log(e)
        });

 注意点:

        1.设置offset: new AMap.Pixel(-20, -62)

        2.设置imageSize: new AMap.Size(15, 21)

        3.设置icon: new AMap.Icon({ })

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要让 Vue 中的高德地图标记点击后跳动一下,可以利用高德地图 JavaScript API 的 Marker 类和 Animation 类来实现。 首先,在 Vue 中引入高德地图 JavaScript API: ```html <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 然后,在 Vue 组件中创建地图标记: ```vue <template> <div ref="mapContainer" style="height: 400px;"></div> </template> <script> export default { data() { return { map: null, marker: null }; }, mounted() { this.initMap(); }, methods: { initMap() { this.map = new window.AMap.Map(this.$refs.mapContainer, { center: [116.397428, 39.90923], zoom: 13 }); this.marker = new window.AMap.Marker({ position: [116.397428, 39.90923], map: this.map, animation: 'AMAP_ANIMATION_DROP' }); this.marker.on('click', () => { this.marker.setAnimation('AMAP_ANIMATION_BOUNCE'); }); } } }; </script> ``` 在上面的代码中,首先在 mounted 钩子函数中调用 initMap 方法创建地图标记。在 initMap 方法中,我们通过创建 AMap.Map 实例来创建地图,并且通过创建 AMap.Marker 实例来创建标记。在创建标记的时候,我们设置了 animation 属性为 AMAP_ANIMATION_DROP,这样标记就会在创建时从上掉下来。然后通过监听标记的 click 事件,在回调函数中设置 animation 属性为 AMAP_ANIMATION_BOUNCE,这样标记就会跳动起来。 注意,要让标记跳动起来,需要在创建标记时设置 animation 属性为 AMAP_ANIMATION_BOUNCE。另外,在改变标记的动画效果时,需要通过 setAnimation 方法来设置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值