vue 高德地图AMap.MassMarks添加海量点标记、AMap.InfoWindow创建自定义信息窗体

该博客介绍了如何在Vue项目中使用高德地图API进行海量点标记的添加和自定义信息窗体的创建。通过预加载地图,动态加载数据并设置标记样式,实现了地图上的点标记功能。同时,利用模板和组件化思想封装了信息窗体,方便复用。此外,详细说明了geoJsonData数据格式,并展示了信息窗体内容的生成方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高德地图添加海量点标记和创建自定义信息窗体的封装

假设已经正确引入了高德地图。那后面就直接看代码

<template>
	<div class="amap" id="amap"></div>
</template>
<script>
import mapFile from '../../../assets/images/map-filling.png'
import mapInfoWindow from "./info-window/info-window";
import reqMapList from '../../../api'
export default{
	data(){
		return {
			center:[12.32652,20.99399]
		}
	},
	mounted(){
		//由于高德地图加载会有延迟,因此需要判断高德地图是否已经加载,若没有加载,则延迟500毫秒加载。
		         this.$nextTick(()=>{
                    if(!window.Amap){
                        setTimeout(()=>{
                            this.loadMap()
                            this.initInfoWindow()
                        },500)
                        return
                    }else{
                        this.loadMap()
                    }
                    this.initInfoWindow()
                })
	},
	methods:{
	       initInfoWindow(){
                // //添加供应商点击事件和关闭点击事件
                window.closeInfoWindow=this.closeInfoWindow
                // //将信息窗体的样式添加到页面
                this.$nextTick(()=>{
                    let mapInfoWindowBox=this.$refs.mapInfoWindowBox
                    let mapInfoWindowStyle=mapInfoWindowBox.querySelector('#mapInfoWindowStyle')
                    if(!mapInfoWindowStyle){
                        let style=document.createElement('style')
                        style.setAttribute('id','mapInfoWindowStyle')
                        style.innerHTML=mapInfoWindow.style
                        mapInfoWindowBox.appendChild(style)
                    }

                })
            },
      async loadMap() { // 加载地图 需要在挂载后
	      let map = new AMap.Map('amap', {
	        center: this.center,
	        zoom: 16,
	      })
	      let {geoJsonData,mapAllList}=await reqMapList()
	      // //默认加载项目的经纬度图标
	      this.addMark(map,{geoJsonData,mapAllList})
	      // //创建信息窗体
	      this.createInfoWindow()
	      map.on('click',(event)=>{
	        // console.log(event)
	      })
	      this.map=map
    },
	 //添加标记
            addMark({geoJsonData,mapAllList}){
                let style=mapAllList.reduce((prev,current)=>{
                    let {province,city,strictName}=current
                    prev.push({
                        url:mapFile,  // 图标地址
                        size: new AMap.Size(30,30),      // 图标大小
                        anchor: new AMap.Pixel(15,25), // 图标显示位置偏移量,基准点为图标左上角
                        title:province+city+strictName
                    })
                    return prev
                },[])
                let massMarks = new AMap.MassMarks(null,{
                    zIndex: 50000,  // 海量点图层叠加的顺序
                    zooms: [3, 19],  // 在指定地图缩放级别范围内展示海量点图层
                    style  // 设置样式对象
                })
                console.log(geoJsonData)
                massMarks.setData(geoJsonData)
                // 将海量点添加至地图实例
                massMarks.setMap(map)
                massMarks.on('click',e=>{
					  this.infoWindow.setContent(mapInfoWindow.template(markEvent.data))
                	 this.infoWindow.open(map,markEvent.data.lnglat)
				})
            },
              //创建信息窗体
            createInfoWindow(){
                this.infoWindow=new AMap.InfoWindow({
                    content:'',  //传入 dom 对象,或者 html 字符串
                    isCustom: true,  //使用自定义窗体
                    autoMove:true,
                    anchor:'bottom-right',
                    offset:new AMap.Pixel(-20,-20),
                })
            },
              //关闭信息窗体
	    closeInfoWindow(){
	      this.infoWindow.close()
	    },
}
</script>

这里说下geoJsonData的数据格式,数据结构如下:

[
	{
          lnglat:[lon,lat],
          style:index,
        }
]

这是基本的数据结构,至于是否需要其他数据,可以视情况自己添加。创建海量点标记可以单独给每个点标记创建样式,style用来指定使用使用哪一个点标记,因此值index就是数组下标。lnglat是经纬度数组。

然后看下对Infowindow的封装。

export default {
  template:(data)=>{
    let {lnglat,name,fullAddress,area,amount,phone,type}=data
    return `<div class="infoWindowBox">
                       <div class="rowStart itemBox">
            <i class="iconfont iconjianzhu1"></i>
            <span>${name}</span>
          </div>
          <div class="rowStart itemBox">
            <div class="childItemBox rowStart">
              <i class="iconfont iconpingfangmi"></i>
              <span class="itemText">${area} ㎡</span>
            </div>
            <div class="childItemBox rowStart">
              <i class="iconfont iconjine"></i>
              <span class="itemText">${amount} 万元</span>
            </div>
          </div>
          <div class="rowStart itemBox">
            <div class="childItemBox rowStart">
              <i class="iconfont mapWidowIcon icongangcai"></i>
              <span class="itemText">${type || ''}</span>
            </div>
            <div class="childItemBox rowStart">
              <i class="iconfont iconkefudianhua"></i>
              <span class="itemText">${phone}</span>
            </div>
          </div>
          <div class="rowStart itemBox">
            <i class="iconfont icondizhi"></i>
            <span class="itemText">${fullAddress}</span>
          </div>
          <i class="el-icon-close" id="infoWindowClose" οnclick="closeInfoWindow()"></i>
                    </div>`
  },
  style:`
.infoWindowBox{
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 2px 3px 8px 0px rgba(0, 0, 0, 0.35);
  border-radius: 4px;
  padding:40px 40px 30px 40px;
  position: relative;
  box-sizing: border-box;
}
.itemBox{
 margin-bottom: 10px;
 align-items:flex-start;
}
.childItemBox{
  flex:1;
  align-items:stretch;
}
 .mapWidowIcon{
  font-size: 16px;
  margin-right: 10px;
}
 .itemText{
  font-size: 14px;
}
.infoWindowBox #infoWindowClose{
  position: absolute;
  right:10px;
  top:10px;
  font-size: 28px;
  cursor:pointer;
  color:#666666;
}
`
}

其实就是吧传入infowindow的content属性html抽成js组件而已,只是为了方便复用。

### 配置标记并展示信息窗高德地图 API 中,为了实现标记后显示信息窗的功能,需先初始化地图实例,并在此基础上添加标记。对于每一个希望添加到地图上的,可以利用 `AMap.Marker` 方法来创建标记对象[^1]。 当涉及到自定义这些标记的外观时,则可以通过传递给 `AMap.Marker` 的选项参数中的 `icon` 属性指定图标图像路径或通过 `content` 设置 HTML 片段作为标记内容[^2]。下面是一个简单的例子: ```javascript // 创建地图实例 var map = new AMap.Map('container', { zoom: 10, center: [116.397428, 39.90923], // 地图中心坐标 }); // 定义一个带有自定义样式的 Marker var marker = new AMap.Marker({ position: new AMap.LngLat(116.397428, 39.90923), // 经纬度位置 title: '北京', content: '<div style="background-color:red;width:20px;height:20px;border-radius:50%;"></div>', // 自定义样式 offset: new AMap.Pixel(-10, -20), }); marker.setMap(map); // 将Marker 添加至地图上 // 实现击事件触发 InfoWindow 显示 var infoWindow = new AMap.InfoWindow({offset:new AMap.Pixel(0,-30)}); infoWindow.setContent('<strong>这是来自InfoWindow的信息</strong>'); marker.on('click', function() { infoWindow.open(map, marker.getPosition()); }); ``` 上述代码片段展示了如何向地图中加入具有特定样式的标记,并为其绑定击监听器,在用户单击该标记时打开包含定制化消息的信息窗口。 #### 关联 Vue.js 应用程序 如果是在基于 Vue.js 构建的应用里集成此功能,除了以上步骤外还需要考虑组件生命周期钩子函数内的逻辑处理方式。通常情况下会在 mounted 生命周期阶段完成地图及其元素的渲染工作;另外也可以借助于 vue-amap 这样的第三方库简化操作流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值