vue项目高德地图:循环显示marker点,自定义点图标,点击marker事件

var map = new AMap.Map('videoMap', {
    zoom: 17,
    pitch: 25,
    viewMode: '3D',
    resizeEnable: true,
    layers: [new AMap.TileLayer.Satellite()],   //地图层
  })
	
  //设备数据
  let ddc = marker1.ddc;

  //设备图标
  let ddcImgsrc = require('@/assets/gif/12.gif');
	
  //遍历显示marke
  ddc.forEach(item=>{
    if(!item.lng || !item.lat){return};
    let marker = new AMap.Marker({
      map:map,
      position:[item.lng,item.lat],
      content: `<div><img class="imgRotate" src="${ddcImgsrc}" style="width:30px;height:30px;"/></div>`, //设置文本标注内容
	  offset: new AMap.Pixel(-15, -15),  //此处应设为图标尺寸的一半
      title:`${item.address}`  //鼠标滑过设备显示的文本
   });
   //给marker对象添加一个属性
   marker.setExtData(item);
   marker.on('click', _this.markerClick);	
   //给marker设置一个label
   marker.setLabel({
	  offset:new AMap.Pixel(0,0);
	  content:`<div class="infoInit">${item.name}</div>`,
	  direction:'buttom'  //显示的位置
   })
});
  	
 

  //设置地图层级与中心点  【针对有固定的重心点】
  map.setCenter([lng,lat]); 

  //自动设置中心点	【自动将所有的marker点显示到视觉页面内】
  // 第一个参数为空,表明用图上所有覆盖物 setFitview
  // 第二个参数为false, 非立即执行
  // 第三个参数设置上左下右的空白
  map.setFitView(null,false,[100,60,100,60]);
  

效果图:
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要清除高德地图的搜索结果 Marker,您可以通过以下步骤实现: 1. 在 Vue 组件中引入高德地图 JavaScript API: ``` <script src="https://webapi.amap.com/maps?v=1.4.15&key=YOUR_API_KEY"></script> ``` 2. 在 `mounted` 钩子函数中初始化地图,并将地图实例保存在 `this.map` 中: ``` mounted() { this.map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); } ``` 3. 在搜索时,使用 `AMap.Marker` 创建 Marker,并保存在数组中: ``` search() { // 清除之前的 Marker this.clearMarkers(); // 搜索 AMap.plugin('AMap.PlaceSearch', () => { const placeSearch = new AMap.PlaceSearch({ city: '北京市', pageSize: 10, pageIndex: 1, map: this.map }); placeSearch.search('餐厅', (status, result) => { if (status === 'complete' && result.info === 'OK') { const poiList = result.poiList; poiList.forEach(poi => { const marker = new AMap.Marker({ position: poi.location, title: poi.name, map: this.map }); this.markers.push(marker); }); } }); }); }, ``` 4. 在清除搜索结果时,遍历 Marker 数组,调用 `setMap(null)` 方法将 Marker 从地图上移除,并清空数组: ``` clearMarkers() { this.markers.forEach(marker => { marker.setMap(null); }); this.markers = []; }, ``` 完整的 Vue 组件代码如下: ``` <template> <div> <div id="map-container"></div> <button @click="search">搜索餐厅</button> <button @click="clearMarkers">清除搜索结果</button> </div> </template> <script> export default { data() { return { map: null, markers: [] }; }, mounted() { this.map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); }, methods: { search() { // 清除之前的 Marker this.clearMarkers(); // 搜索 AMap.plugin('AMap.PlaceSearch', () => { const placeSearch = new AMap.PlaceSearch({ city: '北京市', pageSize: 10, pageIndex: 1, map: this.map }); placeSearch.search('餐厅', (status, result) => { if (status === 'complete' && result.info === 'OK') { const poiList = result.poiList; poiList.forEach(poi => { const marker = new AMap.Marker({ position: poi.location, title: poi.name, map: this.map }); this.markers.push(marker); }); } }); }); }, clearMarkers() { this.markers.forEach(marker => { marker.setMap(null); }); this.markers = []; }, } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值