高德地图选择点位改变图标

一.循环渲染点位

如图所示,紫色点位为动态循环点位

首先,我们请求完接口,返回点位

这里是全部代码  下方有拆分讲解

this.map.remove(this.marks)
this.marks = []
this.map.plugin(['AMap.PlaceSearch'], () => {
    var PlaceSearchOptions = { //设置PlaceSearch属性
        city: "武汉", //城市
        type: "", //数据类别
        pageSize: 10, //每页结果数,默认10
        pageIndex: 1, //请求页码,默认1
        extensions: "base" //返回信息详略,默认为base(基本信息)
    };
    var MSearch = new this.amap.PlaceSearch(PlaceSearchOptions); //构造PlaceSearch类
    this.amap.Event.addListener(MSearch, "complete", (res) => {
        if (res.poiList.pois && res.poiList.pois.length) {
            this.map.setZoomAndCenter(15, [res.poiList.pois[0].location.lng, res.poiList.pois[0].location.lat])
            res.poiList.pois.forEach(item => {
                let marker = new this.amap.Marker({
                    position: [item.location.lng, item.location.lat],
                    icon: new this.amap.Icon({
                        image: require("@/assets/ple.png"),
                    })
                })
                this.marks.push(marker)
                marker.on('click', () => {
                    twParkingInfo({
                        longitude: item.location.lng,
                        latitude: item.location.lat
                    }).then(({ data }) => {
                        this.praking = data.data
                        this.isShow = true
                        this.marks.forEach((item, index) => {
                            item.setIcon(new this.amap.Icon({
                                image: require("@/assets/ple.png"),
                            }))
                        })
                        marker.setIcon(new this.amap.Icon({
                            image: require("@/assets/cs.png"),
                        }))
                        this.map.setZoomAndCenter(15, [item.location.lng, item.location.lat])
                    })

                })
                this.map.add(marker);

            });
        }
    }); //返回结果
    MSearch.search(this.input); //关键字查询
});

这里我调用了高德的查询 API 搜索后点击搜索按钮事件 循环高德返回点位

循环添加点位

res.poiList.pois.forEach(item => {
    let marker = new this.amap.Marker({
        position: [item.location.lng, item.location.lat],
        icon: new this.amap.Icon({
            image: require("@/assets/ple.png"),
        })
    })
    this.map.add(marker);

});

二.定义数组接受新增点位

data 里面定义一个数组 我的叫  marks 

这里在循环的时候将新增的 marker 全都 pushmarks

this.marks.push(marker)

这里添加到数组里有两点好处

1. 便于下次搜索添加新点位之前  清除掉上次搜索增加点位

this.map.remove(this.marks) // 清除点位
this.marks = []

所以每次调用查询方法时  将之前增加点位移除  并将该数组清空

2. 便于修改图标

marker.on('click', () => {
    // 每次点击  将点位数组循环  将图标修改成统一图标
   this.marks.forEach((item, index) => {
      item.setIcon(new this.amap.Icon({
         image: require("@/assets/ple.png"),
      }))
   })
   // 然后单独修改点击点位图标 即可 
   marker.setIcon(new this.amap.Icon({
       image: require("@/assets/cs.png"),
   }))
   this.map.setZoomAndCenter(15, [item.location.lng, item.location.lat])
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值