vue[高德地图自定义InfoWindow弹出框中@click事件问题]

问题

在地图中有时候我们会遇上那种需要直接添加页面元素的操作内容。如:
在这里插入图片描述
我们给InfoWindow添加了一些页面内容使其在地图效果如下:
在这里插入图片描述
然后给按钮绑定了点击事件,如:
在这里插入图片描述
但是这样的点击确是无效的
在这里插入图片描述
然而我们确需要在本组件中自定义一个窗体内容并且给按钮绑定一个本组件能够访问事件。

解决方式

在vue中使用 vue.extend() 来生成html可以解决自定义元素添加事件的问题,并且可以访问本组件中的事件、数据或者进行路由跳转
备注:Vue.extend()和vue的组件很类似,其内容如:data(),methods,生命周期…等和组件是一样的。

在这里插入图片描述
代码:

<template>
  <div class="login-box" id="login-box"></div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      map: null,
      infoWindow: null,
      testOut: "这是外面的内容",
    };
  },
  mounted() {
    this.$map().then((AMap) => {
      this.map = new AMap.Map("login-box", {
        center: [117.00923, 36.675807],
        zoom: 11,
      });
      this.infoWindow = new AMap.InfoWindow({
        isCustom: true,
        offset: new AMap.Pixel(16, -45),
      });
      // 提示:这里必须要保存一下this,在访问extend外部的内容时候需要:如 that.testOut
      let that = this;
      let InfoContent = Vue.extend({
        template: `<div class="login-infowindow">
          <h2>自定义标题</h2>
          <button @click='clickA'>点击事件</button>
          <button @click='clickB'>点击路由跳转</button>
        </div>`,
        data() {
          return {
            testIner: "这是里面的内容",
          };
        },
        methods: {
          clickA() {
            console.log("123");
            console.log(that.testOut);
            console.log(this.testIner);
          },
          clickB() {
            console.log("clickB");
            that.$router.push("/home");
          },
        },
      });
      let component = new InfoContent().$mount();
      this.infoWindow.setContent(component.$el);
      this.infoWindow.open(this.map, [117.00923, 36.675807]);
    });
  },
};
</script>

<style lang="scss">
.login-box {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  .login-infowindow {
    width: 200px;
    height: 100px;
    background: #fff;
  }
}
</style>

效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
只要能够通过that访问组件的外部内容必然就能访问$router这个对象,因此就可以进行跳转,否则请检查一下你的代码是否出现的一些阻塞的问题。

提示

在这里插入图片描述

在这里插入图片描述

报错解决:
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Vue高德地图自定义标记可以通过以下步骤实现: 1. 在Vue组件中引入高德地图API: ``` import AMapLoader from '@amap/amap-jsapi-loader'; ``` 2. 在Vue组件中定义地图容器,并在mounted钩子函数中加载地图: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, }, }; </script> ``` 3. 在Vue组件中定义自定义标记的样式,并使用AMap.Marker类创建自定义标记对象: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, markerStyle: { width: '24px', height: '24px', background: 'url("your-marker-icon.png") no-repeat', backgroundSize: '100% 100%', }, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); this.addMarker(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, addMarker() { const marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map, icon: new AMap.Icon(this.markerStyle), }); }, }, }; </script> ``` 4. 根据需要,可以在自定义标记上添加事件监听器: ``` <template> <div id="map-container"></div> </template> <script> export default { data() { return { map: null, markerStyle: { width: '24px', height: '24px', background: 'url("your-marker-icon.png") no-repeat', backgroundSize: '100% 100%', }, }; }, mounted() { AMapLoader.load({ key: 'your-api-key', version: '2.0', plugins: ['AMap.Marker'], }).then(() => { this.initMap(); this.addMarker(); }); }, methods: { initMap() { this.map = new AMap.Map('map-container', { center: [116.397428, 39.90923], zoom: 13, }); }, addMarker() { const marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map, icon: new AMap.Icon(this.markerStyle), }); marker.on('click', () => { alert('Marker clicked!'); }); }, }, }; </script> ``` 以上就是Vue高德地图自定义标记的实现方法。需要注意的是,自定义标记的样式需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值