vue 高德地图点击标记点弹出对应信息弹窗

新建文件 amap.vue

<template>
  <div id="amapcontainer" style="width: 1200px; height: 720px"></div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
  securityJsCode: '' // '「申请的安全密钥」',
}
export default {
  data () {
    return {
      map: null,
      markerList: [],
      infoWindow: '',
      mapList: [
        {
          name: '小王',
          address: '广东省广州市海珠区',
          lnglats: [113.312566, 23.085073]
        }, {
          name: '小张',
          address: '广东省广州市黄埔区',
          lnglats: [113.480794, 23.137896]
        }, {
          name: '小李',
          address: '广东省广州市荔湾区',
          lnglats: [113.220556, 23.10718]
        },
        {
          name: '小赵',
          address: '广东省广州市天河区',
          lnglats: [113.365438, 23.124231]
        }
      ]
    }
  },
  mounted () {
    // DOM初始化完成进行地图初始化
    this.initAMap()
  },
  methods: {
    initAMap () {
      AMapLoader.load({
        key: "", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.ControlBar", 'AMap.Geocoder', 'AMap.Marker',
          'AMap.CitySearch', 'AMap.Geolocation', 'AMap.AutoComplete', 'AMap.InfoWindow'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        // 获取到作为地图容器的DOM元素,创建地图实例
        this.map = new AMap.Map("amapcontainer", { //设置地图容器id
          resizeEnable: true,
          zoom: 10, // 地图显示的缩放级别
          viewMode: "3D", // 使用3D视图
          zoomEnable: true, // 地图是否可缩放,默认值为true
          dragEnable: true, // 地图是否可通过鼠标拖拽平移,默认为true
          doubleClickZoom: true, // 地图是否可通过双击鼠标放大地图,默认为true
          zoom: 11, //初始化地图级别
          center: [113.370824, 23.131265], // 初始化中心点坐标 广州
          // mapStyle: "amap://styles/darkblue", // 设置颜色底层
        })
        this.setMapMarker()
      }).catch(e => {
        console.log(e)
      })
    },
    // 增加点标记
    setMapMarker () {
      // 创建 AMap.Icon 实例
      let icon = new AMap.Icon({
        size: new AMap.Size(36, 36), // 图标尺寸
        image: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png", // Icon的图像
        imageSize: new AMap.Size(24, 30), // 根据所设置的大小拉伸或压缩图片
        imageOffset: new AMap.Pixel(0, 0)  // 图像相对展示区域的偏移量,适于雪碧图等
      });
      //信息窗口实例
      this.infoWindow = new AMap.InfoWindow({
        anchor: "top-left",
        offset: new AMap.Pixel(0, -30)
      });
      let makerList = []
      this.mapList.forEach((item, i) => {
        // 遍历生成多个标记点
        let marker = new AMap.Marker({
          map: this.map,
          zIndex: 9999999,
          icon: icon, // 添加 Icon 实例
          offset: new AMap.Pixel(-13, -30), //icon中心点的偏移量
          position: item.lnglats // 经纬度对象new AMap.LngLat(x, y),也可以是经纬度构成的一维数组[116.39, 39.9]
        });
        let content =
          '<ul style="  margin:-10px 0 5px 0; padding:0.2em 0;">'
          + '<li  style="font-size:14px;color:#727272;">'
          + '<span style="width:50px; display:inline-block;">第</span>'
          + i + '条</li>'
          + '<li style="line-height:26px; font-size:14px;color:#727272;margin-bottom:6px">'
          + '<span style="width:50px; display:inline-block;">姓 名:</span>'
          + item.name + '</li>'
          + '<li style="font-size:14px;color:#727272;">'
          + '<span style="width:50px; display:inline-block;" >地 址:</span>'
          + item.address + '</li>'
          + '</ul>'
        marker.content = content
        marker.on("click", this.markerClick)
        marker.emit('click', { target: marker });// 此处是设置默认出现信息窗体
        makerList.push(marker)
      });
      this.makerList = makerList
      this.map.add(makerList)
      // 自动适应显示想显示的范围区域
      this.map.setFitView();
    },
    // 控制标记的信息窗体的显示
    markerClick (e) {
      // 标注的点击事件
      this.infoWindow.setContent(e.target.content);
      this.infoWindow.open(this.map, e.target.getPosition());
    }
  }
}
</script>

<style lang="less">
</style>

在需要使用的组件中引入 amap.vue

<template>
  <div>
    <map-container></map-container>
  </div>
</template>
<script>
import MapContainer from "@/components/amap";
export default {
  name: "purchannel",
  components: { MapContainer },
  data () {
    return {
    }
  },
  watch: {},
  created () { },
  mounted () { },
  methods: {
  }
}
</script>

<style lang="less" scoped>
</style>

页面效果:
请添加图片描述

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要让 Vue 中的高德地图标记点击后跳动一下,可以利用高德地图 JavaScript API 的 Marker 类和 Animation 类来实现。 首先,在 Vue 中引入高德地图 JavaScript API: ```html <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 然后,在 Vue 组件中创建地图和点标记: ```vue <template> <div ref="mapContainer" style="height: 400px;"></div> </template> <script> export default { data() { return { map: null, marker: null }; }, mounted() { this.initMap(); }, methods: { initMap() { this.map = new window.AMap.Map(this.$refs.mapContainer, { center: [116.397428, 39.90923], zoom: 13 }); this.marker = new window.AMap.Marker({ position: [116.397428, 39.90923], map: this.map, animation: 'AMAP_ANIMATION_DROP' }); this.marker.on('click', () => { this.marker.setAnimation('AMAP_ANIMATION_BOUNCE'); }); } } }; </script> ``` 在上面的代码中,首先在 mounted 钩子函数中调用 initMap 方法创建地图和点标记。在 initMap 方法中,我们通过创建 AMap.Map 实例来创建地图,并且通过创建 AMap.Marker 实例来创建点标记。在创建点标记的时候,我们设置了 animation 属性为 AMAP_ANIMATION_DROP,这样点标记就会在创建时从天上掉下来。然后通过监听点标记的 click 事件,在回调函数中设置 animation 属性为 AMAP_ANIMATION_BOUNCE,这样点标记就会跳动起来。 注意,要让点标记跳动起来,需要在创建点标记时设置 animation 属性为 AMAP_ANIMATION_BOUNCE。另外,在改变点标记的动画效果时,需要通过 setAnimation 方法来设置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值