我想在我的传单 map 中添加和删除标记.我发现这个主题很好,但是代码是JS的.

Leaflet - How to find existing markers, and delete markers?

我的代码是这样的:

<template>
 <l-map :zoom="zoom" :center="center">
  <l-tile-layer :url="url" :attribution="attribution"></l-tile-layer>
  <l-marker :lat-lng="marker" :draggable=true></l-marker>
 </l-map>
</template>

<script>
data:function() {
        return {
  zoom:7,
  center: L.latLng(33.901445, -5.532788),
  url:'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
  attribution:'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> 
  contributors',
  marker: L.latLng(47.413220, -1.219482),
  }},
</script>

也许我应该先在单击时创建一个函数,如下所示:

<l-map :zoom="zoom" :center="center" v-on:click="addMarker"></l-map>

然后我在方法中编写了正确的addMarker函数.但我找不到合适的doctor .

我还想知道新标记在数据中的位置..

谢谢你

推荐答案

事实证明这真的很简单.使用带有v-for的数组,而不是单个标记.单击"标记"可在该索引处拼接出标记.点击 map 创建一个带有板条的新标记.下面的片段基于this fiddle.

var {
  LMap,
  LTileLayer,
  LMarker
} = Vue2Leaflet;

new Vue({
  el: '#app',
  components: {
    LMap,
    LTileLayer,
    LMarker
  },
  data() {
    return {
      zoom: 14,
      center: L.latLng(47.413220, -1.219482),
      url: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
      markers: [
        L.latLng(47.412, -1.218),
        L.latLng(47.413220, -1.219482),
        L.latLng(47.414, -1.22),
      ]
    }
  },
  methods: {
    removeMarker(index) {
      this.markers.splice(index, 1);
    },
    addMarker(event) {
      this.markers.push(event.latlng);
    }
  }
});
html, body, #app {
  height: 100%;
  margin: 0;
}
<script src="https://unpkg.com/vue@latest/dist/vue.js"></script>
<link href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" rel="stylesheet" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<script src="https://unpkg.com/vue2-leaflet@1.0.1/dist/vue2-leaflet.js"></script>
<div id="app">
  <l-map :zoom="zoom" :center="center" @click="addMarker">
    <l-tile-layer :url="url" :attribution="attribution"></l-tile-layer>
    <l-marker v-for="marker, index in markers" :lat-lng="marker" @click="removeMarker(index)"></l-marker>
  </l-map>
</div>
<

Vue.js相关问答推荐

VueI18n 无法读取或更改组合 API 中的区域设置

如何在 Vuetify 2 中的轮播项目上放置 href 属性?

在 VueJS 中为 Get、Post、Patch 配置全局标头的最佳方法

aspnet core如何在deploy上构建webpack

如何为 Vue 应用提供 robots.txt

如何在 Vue.js 中使用 MaterializeCss?

在 v-for 的情况下如何从 v-model 输入更新 Vuex 存储

如何将 mapActions 与 vue + Typescript 类组件一起使用?

使用 vue-test-utils 配置过滤器

vue-router的router-link实际刷新?

错误:vue-loader 要求 @vue/compiler-sfc 存在于依赖树中

如何从 vue.js 捕获 Jquery 事件

如何使用 JavaScript 按索引删除数组元素?

如何在两个属性上使用 Vue/Vuetify 自动完成过滤器?

Vue - 如何在 v-if 或组件中使用窗口对象

如何使用vue.js/nuxt.js获取一个目录下的所有图片文件

VueJS错误避免直接改变props

错误:找不到模块'@vue/babel-preset-app'

v-for 中的计算(Computed)/动态(Dynamic) v-model 名称

如何隔离 Vuetify 全局样式