在Leaflet地图中插入上下文菜单

huangapple go评论67阅读模式
英文:

Insert context menu in Leaflet map

问题

我想询问关于Vue 3的问题,如何在Leaflet地图中插入自定义上下文菜单?我正在使用leaflet@1.9.4,@vue-leaflet/vue-leaflet@0.10.1,并尝试使用leaflet-contextmenu@1.4.0。我的代码如下:

<template>
	<div class="map">
		<l-map ref="map" v-model:zoom="zoom" :center="[49.2265592, 16.5957531]">
			<l-tile-layer
				url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
				layer-type="base"
				name="OpenStreetMap"
			></l-tile-layer>
		</l-map>
	</div>
</template>

<script>
import "leaflet/dist/leaflet.css";
import { LMap, LTileLayer } from "@vue-leaflet/vue-leaflet";
// import L from 'leaflet';
// import "leaflet-contextmenu";

export default {
	components: {
		LMap,
		LTileLayer,
	},
	data() {
		return {
			zoom: 13,
		};
	},
};
</script>

我想要做的事情类似于:

L.map.contextmenu = true;
L.map.contextmenuItems = [{
	text: 'Show coordinates',
}];

然而,我不知道在哪里以及如何编写这段代码。任何反馈将不胜感激。

英文:

I want to ask about Vue 3, how to insert a custom context menu in a Leaflet map?
I'm using leaflet@1.9.4, @vue-leaflet/vue-leaflet@0.10.1 and trying to use leaflet-contextmenu@1.4.0.
My code looks like this:

&lt;template&gt;
	&lt;div class=&quot;map&quot;&gt;
		&lt;l-map ref=&quot;map&quot; v-model:zoom=&quot;zoom&quot; :center=&quot;[49.2265592, 16.5957531]&quot;&gt;
			&lt;l-tile-layer
				url=&quot;https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png&quot;
				layer-type=&quot;base&quot;
				name=&quot;OpenStreetMap&quot;
			&gt;&lt;/l-tile-layer&gt;
		&lt;/l-map&gt;
	&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import &quot;leaflet/dist/leaflet.css&quot;;
import { LMap, LTileLayer } from &quot;@vue-leaflet/vue-leaflet&quot;;
// import L from &#39;leaflet&#39;;
// import &quot;leaflet-contextmenu&quot;;

export default {
	components: {
		LMap,
		LTileLayer,
	},
	data() {
		return {
			zoom: 13,
		};
	},
};
&lt;/script&gt;

What I would like to do is something like:

L.map.contextmenu = true;
L.map.contextmenuItems = [{
	text: &#39;Show coordinates&#39;,
}];

However, I can't figure out how and where to write this.
Any feedback would be appreciated.

答案1

得分: 0

抱歉,我无法提供代码的翻译。以下是您提供的内容中的翻译部分:

"Unfortunately I cant tell you much about @vue-leaflet/vue-leaflet directly, since I never used it. However, you said "Any feedback would be appreciated.", so I'd like to present you a slightly different approach.
通常,我会直接在我的Vue应用程序中包含Leaflet,就像这样:

首先,我会创建一个对地图元素的ref - 就像您所做的一样:

<div ref="mapEl"></div>

然后,我使用Vue的ref函数获取该引用:

import { ref } from 'vue';
const mapEl = ref();

您可以直接将引用的值(包含HTMLElement)传递给Leaflet的构造函数。我会在onMounted钩子中执行此操作:

const map = leaflet.map(mapEl.value, {
    contextmenu: true,
    contextmenuWidth: 140,
    contextmenuItems: [
      {
        text: 'Show coordinates',
        callback: () => alert('Coordinates callback')
      }
    ]
});
// 其他您想要应用的选项

不要忘记导入Leaflet和contextmenu插件:

import leaflet from 'leaflet';
import 'leaflet-contextmenu';
import 'leaflet-contextmenu/dist/leaflet.contextmenu.min.css';

完整的代码可能如下所示:

<template>
  <div ref="mapEl"></div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import leaflet from 'leaflet';
import 'leaflet-contextmenu';
import 'leaflet-contextmenu/dist/leaflet.contextmenu.min.css';

const mapEl = ref();

onMounted(() => {
  const map = leaflet.map(mapEl.value, {
    contextmenu: true,
    contextmenuWidth: 140,
    contextmenuItems: [
      {
        text: 'Show coordinates',
        callback: () => alert('Coordinates callback')
      }
    ]
  });
  // 其他选项您想要应用的选项
});
</script>

希望这能解决您的问题。祝您的应用程序好运! 在Leaflet地图中插入上下文菜单

英文:

Unfortunately I cant tell you much about @vue-leaflet/vue-leaflet directly, since I never used it. However, you said "Any feedback would be appreciated.", so I'd like to present you a slightly different approach.
Usually I include Leaflet directly in my Vue Apps like this:

First I create a ref to my map element - just like you did:

&lt;div ref=&quot;mapEl&quot;&gt;&lt;/div&gt;

Then I get that ref using Vues ref function:

import { ref } from &#39;vue&#39;;
const mapEl = ref();

You can pass the refs value - which contains the HTMLElement - directly in Leaflets constructor. I'm doing this right in the onMounted hook

const map = leaflet.map(mapEl.value, {
    contextmenu: true,
    contextmenuWidth: 140,
    contextmenuItems: [
      {
        text: &#39;Show coordinates&#39;,
        callback: ()=&gt; alert(&#39;Coordinates callback&#39;)
      }
    ]
});
// Other options you want to apply

Don't forget to import Leaflet and the contextmenu plugin:

import leaflet from &#39;leaflet&#39;;
import &#39;leaflet-contextmenu&#39;;
import &#39;leaflet-contextmenu/dist/leaflet.contextmenu.min.css&#39;;

The full code could look like this:

&lt;template&gt;
  &lt;div ref=&quot;mapEl&quot;&gt;&lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { ref, onMounted } from &#39;vue&#39;;
import leaflet from &#39;leaflet&#39;;
import &#39;leaflet-contextmenu&#39;;
import &#39;leaflet-contextmenu/dist/leaflet.contextmenu.min.css&#39;;

const mapEl = ref();

onMounted(()=&gt; {
const map = leaflet.map(mapEl.value, {
  contextmenu: true,
  contextmenuWidth: 140,
  contextmenuItems: [
    {
      text: &#39;Show coordinates&#39;,
      callback: ()=&gt; alert(&#39;Coordinates callback&#39;)
    }
  ]
});
// Other options you want to apply
});
&lt;/script&gt;

I hope this will solve your problem. Good luck on your app! 在Leaflet地图中插入上下文菜单

答案2

得分: 0

我已经弄清楚如何在 @vue-leaflet/vue-leaflet@0.10.1 中使用 leaflet-contextmenu@1.4.0
关键是将 options(`:options="mapOptions")传递给 leaflet 地图构造函数,这可以通过声明式完成。
代码如下:

<template>
	<div class="map">
		<l-map ref="map" v-model:zoom="zoom" :center="[49.2265592, 16.5957531]" :options="mapOptions">
			<l-tile-layer
				url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
				layer-type="base"
				name="OpenStreetMap"
			></l-tile-layer>
		</l-map>
	</div>
</template>

<script>
import "leaflet/dist/leaflet.css";
import { LMap, LTileLayer } from "@vue-leaflet/vue-leaflet";
import 'leaflet-contextmenu';
import 'leaflet-contextmenu/dist/leaflet.contextmenu.min.css';

export default {
	components: {
		LMap,
		LTileLayer,
	},
	data() {
		return {
			zoom: 13,
			mapOptions: {
				contextmenu: true,
				contextmenuWidth: 140,
				contextmenuItems: [
				{
					text: 'Show coordinates',
					callback: ()=> alert('Coordinates callback')
				}
				]
			},
		};
	},
};
</script>

希望这对其他人有所帮助。

英文:

I have figured out how to use leaflet-contextmenu@1.4.0 in @vue-leaflet/vue-leaflet@0.10.1.
The key is passing options (:options=&quot;mapOptions&quot;) to the leaflet map constructor, which can be done declaratively.
The code is like:

&lt;template&gt;
	&lt;div class=&quot;map&quot;&gt;
		&lt;l-map ref=&quot;map&quot; v-model:zoom=&quot;zoom&quot; :center=&quot;[49.2265592, 16.5957531]&quot; :options=&quot;mapOptions&quot;&gt;
			&lt;l-tile-layer
				url=&quot;https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png&quot;
				layer-type=&quot;base&quot;
				name=&quot;OpenStreetMap&quot;
			&gt;&lt;/l-tile-layer&gt;
		&lt;/l-map&gt;
	&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import &quot;leaflet/dist/leaflet.css&quot;;
import { LMap, LTileLayer } from &quot;@vue-leaflet/vue-leaflet&quot;;
import &#39;leaflet-contextmenu&#39;;
import &#39;leaflet-contextmenu/dist/leaflet.contextmenu.min.css&#39;;

export default {
	components: {
		LMap,
		LTileLayer,
	},
	data() {
		return {
			zoom: 13,
			mapOptions: {
				contextmenu: true,
				contextmenuWidth: 140,
				contextmenuItems: [
				{
					text: &#39;Show coordinates&#39;,
					callback: ()=&gt; alert(&#39;Coordinates callback&#39;)
				}
				]
			},
		};
	},
};
&lt;/script&gt;

I hope this helps someone else.

huangapple
  • 本文由 发表于 2023年7月4日 23:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定