高德地图多个点位信息窗体添加

高德地图实现多个点位打点,并进行信息窗体的添加,点击左下角的操作按钮进行信息窗体的动态数据显示

1、多个点位打点

for (var i = 0; i < this.btnList.length; i++) {
  this.marker = new AMap.Marker({
    position: this.btnList[i].position,
    map: this.map,
  });
  this.map.add(this.marker);
}

this.map.setFitView();

2、信息窗体的添加

this.infoWindow = new AMap.InfoWindow({
  offset: new AMap.Pixel(0, -30),
});

全部代码

<template>
  <div class="map-container">
    <div id="amapInfo" class="map"></div>
    <div class="btn-info">
      <div class="btn-lists" v-for="(item, index) in btnList" :key="index">
        <div class="btn" @click="handleClickBtn(item,index)">
          {{ item.title || "--" }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
  securityJsCode: "自己的高德密钥",
};
export default {
  data() {
    return {
      map: null,
      infoWindow: null,
      marker: null,
      activeIndex: 0,
      btnList: [
        {
          position: [103.860504, 36.035],
          title: "店铺一",
          content: {
            title: '店铺一',
            activeIndex: 0,
            subTitle: '店铺一测试',
            subTitleInfo: "店铺一测试info",
            img: require('@/assets/images/echarts/bar5/bar5.png')
          },
          info: {
            target: "",
            type: "click",
          },
        },
        {
          position: [103.860804, 36.035],
          title: "店铺二",
          content: {
            title: '店铺二',
            activeIndex: 1,
            subTitle: '店铺二测试',
            subTitleInfo: '店铺二测试',
            img: require('@/assets/images/echarts/bar5/bar5.png')
          },
          info: {
            target: "",
            type: "click",
          },
        },
        {
          position: [103.862604, 36.035],
          title: "店铺三",
          content: {
            title: '店铺三',
            activeIndex: 2,
            subTitle: '店铺三测试',
            subTitleInfo: '店铺三测试',
            img: require('@/assets/images/echarts/bar5/bar5.png')
          },
          info: {
            target: "",
            type: "click",
          },
        },
        {
          position: [103.862904, 36.035],
          title: "店铺四",
          content: {
            title: '店铺四',
            activeIndex: 3,
            subTitle: '店铺四测试',
            subTitleInfo: '店铺四测试',
            img: require('@/assets/images/echarts/bar5/bar5.png')
          },
          info: {
            target: "",
            type: "click",
          },
        },
      ],
    };
  },
  created() {
    this.$nextTick(() => {
      AMapLoader.reset();
      this.initAMap();
    });
  },
  methods: {
    initAMap() {
      AMapLoader.load({
        key: "自己的高德key", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        // 获取到作为地图容器的DOM元素,创建地图实例
        this.map = new AMap.Map("amapInfo", {
          // 设置地图容器id
          resizeEnable: true,
          viewMode: "3D", // 使用3D视图
          zoomEnable: true, // 地图是否可缩放,默认值为true
          zoom: 12,
          pitch: 40, // 初始俯仰角
          // rotation: -10, // 初始旋转角
          mapStyle: "amap://styles/46f0078af51dddcb9fbd4d44ec0b53da",
        });
        this.infoWindow = new AMap.InfoWindow({
          offset: new AMap.Pixel(0, -30),
        });
        for (var i = 0; i < this.btnList.length; i++) {
          this.marker = new AMap.Marker({
            position: this.btnList[i].position,
            map: this.map,
          });
          // console.log("this.marker",this.marker);
          this.btnList[i].info.target = this.marker;
          this.marker.content = this.btnList[i].content;
          this.marker.on("click", this.markerClick);
          this.marker.emit("click", { target: this.marker });
          this.map.add(this.marker);
        }

        this.map.setFitView();
      });
    },
    markerClick(e) {
      const tempColor = `color${e.target.content.activeIndex + 1}`;
      let contentText = `<div class='map-content'>
        <div class="title">${e.target.content.title}</div>  
        <div class="tagInfo ${tempColor}">${e.target.content.subTitle}</div>  
        <div class="serible">${e.target.content.subTitleInfo}:
          <img class="serible-img" src="${e.target.content.img}" alt="">
          <div class="tag">舒适</div>
        </div>
      </div>`;
      this.infoWindow.setContent(contentText);
      this.infoWindow.open(this.map, e.target.getPosition());
    },
    handleClickBtn(item,index) {
      this.activeIndex = index
      this.markerClick(item.info);
    },
  },
};
</script>

<style lang="scss" scoped>
.map-container {
  width: 100%;
  height: 100%;
  position: relative;

  .map {
    width: 100%;
    height: 100%;
  }

  .btn-info {
    padding: 5px 20px;
    position: absolute;
    bottom: 20px;
    left: 10px;
    background: rgba(0,0,0,0.5);
    .btn-lists {
      .btn {
        padding: 5px 10px;
        cursor: pointer;
        border: 1px solid #ccc;
        margin: 10px 0px;
        color: #fff;
      }
    }
  }
  ::v-deep .amap-info-sharp{
    display: none;
  }
  ::v-deep .amap-info-content {
    // border: 1px solid blue;
    // background: transparent;
    padding: 0px;
    .amap-info-close{
      position: absolute;
      top: 12px;
      color: #ffffff;
      font-size: 12px;
    }
  }
  ::v-deep .map-content {
    width: 160px;
    // height: 2.85rem;
    background: rgba(0, 0, 0, 0.5);
    box-shadow: 0px 2px 3.2px 0.6px rgba(84, 152, 217, 0.5);
    border-radius: 4px;
    color: #ffffff;
    font-size: 18px;
    // z-index: 200 !important;
    display: flex;
    flex-direction: column;
    row-gap: 5px;
    overflow: hidden !important;
    padding: 10px 10px;
    .title {
      font-family: SourceHanSansCN, SourceHanSansCN;
      font-weight: 500;
      font-size: 14px;
      color: #ffffff;
      font-style: normal;
    }
    .tagInfo {
      width: 130px;
      // height: 20px;
      background: #1381d2;
      border-radius: 2px;
      padding: 4px 0px 4px 10px;
      font-size: 10px;
      display: flex;
      align-items: center;
    }
    .address {
      font-family: SourceHanSansCN, SourceHanSansCN;
      font-weight: 400;
      font-size: 10px;
      color: #ffffff;
      font-style: normal;
    }
    .serible {
      font-family: SourceHanSansCN, SourceHanSansCN;
      font-weight: 400;
      font-size: 10px;
      color: #ffffff;
      font-style: normal;
      display: flex;
      align-items: center;
      .tag {
        background: #15b26a;
        border-radius: 3.2px;
        font-family: SourceHanSansCN, SourceHanSansCN;
        font-weight: 400;
        font-size: 10px;
        color: #ffffff;
        font-style: normal;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0.5px 5px;
      }
    }
    .color1 {
      background: #1381d2 !important;
    }
    .color2 {
      background: rgba(22, 153, 130, 1) !important;
    }
    .color3 {
      background: rgba(24, 146, 161, 1);
    }
    .color4 {
      background: rgba(166, 121, 20, 1);
    }
  }
}
</style>

效果图如下:

可以实现左下角的点击联动地图点位的信息窗体变化

参考:给多个点添加信息窗体-信息窗体-示例中心-JS API 2.0 示例 | 高德地图API (amap.com)

仅为本人使用时的写法,仅供参考,若有不对之处请评论指正,感谢

### 如何使用高德地图 API 获取自定义点位 为了在高德地图上显示特定位置并对其进行操作,开发者通常会利用 `Marker` 对象来表示这些地理位置。通过设置不同的属性和调用相应的方法,可以实现对这些标记点的定制化处理。 #### 创建 Map 实例 首先,在页面中初始化一个地图实例,并指定其容器以及初始视图参数: ```javascript // 初始化地图对象,设定地图中心点坐标及缩放级别 var map = new AMap.Map('container', { zoom: 10, center: [116.397428, 39.90923] // 默认北京天安门作为中心点 }); ``` #### 添加自定义 Marker 接着,可以通过创建一个新的 `AMap.Marker()` 来向地图添加自定义的位置标记。这里可以根据实际需求调整图标样式、偏移量等特性[^1]: ```javascript // 定义要添加到地图上的 marker 的配置项 var markerOptions = { position: new AMap.LngLat(116.397428, 39.90923), // 经纬度信息 title: '我的家', }; // 使用上述选项创建 marker 并将其放置于地图之上 var marker = new AMap.Marker(markerOptions); map.add(marker); // 将 marker 加入到地图层中 ``` #### 自定义 Marker 内容 如果希望进一步美化或增强交互验,则可通过修改 `content` 属性来自定义 Marker 显示的内容。这允许嵌入 HTML 片段或其他富媒资源[^2]: ```html <div id="custom-marker"> <img src="./path/to/icon.png"/> </div> ``` ```javascript marker.setLabel({ offset: new AMap.Pixel(-15, 20), content: document.getElementById('custom-marker').innerHTML }); ``` #### 打开信息窗口 当用户点击某个 Marker 后展示更多信息时,可借助 InfoWindow 控件完成此功能。先准备好想要呈现的信息模板,再关联至对应的事件监听器[^3]: ```javascript // 构建 info window 的主结构 var infoWindowContent = '<h4>欢迎来到这里!</h4>' + '<p><strong>地址:</strong>北京市东城区王府井大街8号</p>'; // 新建 info window 实例并将之前准备好的内容赋值进去 var infoWindow = new AMap.InfoWindow({content: infoWindowContent}); // 注册 click 事件处理器以便触发 info window 的弹出动作 marker.on('click', function () { infoWindow.open(map, this.getPosition()); }); ``` 以上就是关于如何运用高德地图 JavaScript API 进行自定义点位的相关介绍与实践案例说明。按照这样的流程即可轻松实现在网页应用内集成个性化地理信息服务的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值