leaflet.js - Set marker on click, update position on drag(Leaflet.js - 点击时设置标记,拖动时更新位置)
问题描述
对于我正在处理的一个小项目,我需要能够在 Leaflet.js 驱动的图像地图上放置一个标记,并在它被拖动时更新该标记的位置.我使用下面的代码来尝试这个,但它失败了.我收到错误未定义标记".我不知道为什么它不起作用 - 也许你们可以帮助我?;)
for a small project I am working on, I need to be able to place a marker on a leaflet.js powered image-map and update the position of this marker, if it gets dragged. I use the following code to try this, but it fails. I get the error 'marker not defined'. I don't know why it's not working - maybe you guys could help me out? ;)
function onMapClick(e) {
gib_uni();
marker = new L.marker(e.latlng, {id:uni, icon:redIcon, draggable:'true'};
map.addLayer(marker);
};
marker.on('dragend', function(event){
var marker = event.target;
var position = marker.getLatLng();
alert(position);
marker.setLatLng([position],{id:uni,draggable:'true'}).bindPopup(position).update();
});
推荐答案
在上面的代码片段中,在添加事件处理程序时没有定义标记.在创建标记后立即添加 dragend 侦听器,请尝试以下操作:
In the code snippet above, marker is not defined at the time the event handler is added. Try the following where the dragend listener is added immediately following the creation of the Marker:
function onMapClick(e) {
gib_uni();
marker = new L.marker(e.latlng, {id:uni, icon:redIcon, draggable:'true'});
marker.on('dragend', function(event){
var marker = event.target;
var position = marker.getLatLng();
console.log(position);
marker.setLatLng(position,{id:uni,draggable:'true'}).bindPopup(position).update();
});
map.addLayer(marker);
};
您的新 L.Marker() 行末尾还缺少一个括号.
You were also missing a bracket at the end of your new L.Marker() line.
您还在 setLatLng
调用中将 position
放入了一个数组中,但它已经是一个 LatLng
对象.
You also put position
into an array in the call to setLatLng
but it is already a LatLng
object.
这篇关于Leaflet.js - 点击时设置标记,拖动时更新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Leaflet.js - 点击时设置标记,拖动时更新位置


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07