h18857377611 2022-02-26 17:05 采纳率: 25%
浏览 177
已结题

vue-amap的marker点单击高亮

我使用vue-amap组件显示marker点,部分代码如下

<el-amap-marker v-for="marker in markers" :key="marker.index" :position="marker.position" :events="marker.events" ></el-amap-marker>

 Point() {
      let markers = [];
      let windows1 = [];
      let srcLists1=[];
      let myicon=[];
      let that = this;
      let afticon=new AMap.Icon({
        //size: new AMap.Size(30, 35),//图标大小
        imageSize:new AMap.Size(35, 35),
        image: "../../static/5.png"
        //image: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png"
      });
      let beficon=new AMap.Icon({
        //size: new AMap.Size(19, 31),//图标大小
        imageSize:new AMap.Size(28, 28),
        image:"../../static/3.png"
        //image: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-blue.png"
      });
      /**function changeIcon(marker) {
        marker.target.setIcon(afticon);
      }**/
      //var showed=e.target.setIcon(afticon);
      this.CollectPoint.forEach((item, index) => {
        //markers.push({icon:beficon});
        markers.push({
          position: item.position,
          offset: new AMap.Pixel(-10, -10),
          icon:beficon, //不设置默认蓝色水滴
          events: {
            click(e) {
               //e.target.setIcon(beficon);
              //this.$refs.map.$$getInstance().setFitView(item.position[index]);
              that.srcList1 = that.srcLists1[index];
              that.marker=that.markers[index];
              //that.window = that.windows[index];
              // 方法:鼠标移动到点标记上,显示相应窗体
              for (let i = 0; i < that.markers.length; i++) {
                that.markers[i].icon=beficon;
              }
              /**that.markers.forEach((itemm)=>{
                  //await itemm;
                  itemm.icon=beficon;
                })**/
              that.windows1.forEach((window1) => {
                window1.visible = false; // 关闭窗体
              });
              that.window1 = that.windows1[index];
              that.$nextTick(() => {
                that.window1.visible = true;
                e.target.setIcon(afticon);
                that.$refs.map.$$getInstance().setZoomAndCenter(6,item.position);
              });
            },
          },
        });

在下方method方法中的显示marker的方法中,点击事件e.target.setIcon(afticon);成功在点击后变换红色图标,结果如下

img

但在点击下一个点后,使得上一个点变回原来的蓝色出现了问题。我的思路是先循环this.markers,把坐标全都变白,然后再setIcon把点击的变红,主体代码就是上面那段代码中的for循环(我也试了foreach),主体代码如下

markers.push({
          position: item.position,
          offset: new AMap.Pixel(-10, -10),
          icon:beficon, //不设置默认蓝色水滴
          events: {
            click(e) {
               //e.target.setIcon(beficon);
              //this.$refs.map.$$getInstance().setFitView(item.position[index]);
              that.srcList1 = that.srcLists1[index];
              that.marker=that.markers[index];
              //that.window = that.windows[index];
              // 方法:鼠标移动到点标记上,显示相应窗体
              for (let i = 0; i < that.markers.length; i++) {
                that.markers[i].icon=beficon;
              }
              /**that.markers.forEach((itemm)=>{
                  //await itemm;
                  itemm.icon=beficon;
                })**/
              that.windows1.forEach((window1) => {
                window1.visible = false; // 关闭窗体
              });
              that.window1 = that.windows1[index];
              that.$nextTick(() => {
                that.window1.visible = true;
                e.target.setIcon(afticon);
                that.$refs.map.$$getInstance().setZoomAndCenter(6,item.position);
              });
            },

但没有用处,点击下一个点上一个点依然是红色,结果如下

img

然后我尝试把循环中的beficon(白色图标)写为afticon(红色图标),点击后所有图标都变红了,如下图

img

然后我尝试把循环放在that.$nextTick内部依然是一样的问题,我感觉是有异步的问题?该怎么解决这个问题呢

  • 写回答

1条回答 默认 最新

  • 你好!机器人 2022-02-26 19:16
    关注

    把下面这行放在for循环之后试下
    that.markers[index].icon=afticon

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月6日
  • 已采纳回答 2月26日
  • 创建了问题 2月26日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格