How do you add marker to map using leaflet map.on(#39;click#39;, function) event handler(如何使用传单 map.on(click, function) 事件处理程序将标记添加到地图)
问题描述
我正在尝试使用事件处理程序向地图添加标记.我可以使用回调函数来管理它,但是当我将函数与事件处理程序分开时就不行了.
I'm trying to use an event handler to add a marker to the map. I can manage this with a callback function, but not when I separate the function from the event handler.
回调(http://fiddle.jshell.net/rhewitt/U6Gaa/7/):
map.on('click', function(e){
var marker = new L.marker(e.latlng).addTo(map);
});
独立函数(http://jsfiddle.net/rhewitt/U6Gaa/6/):
function newMarker(e){
var marker = new L.marker(e.latlng).addTo(map);
}
推荐答案
在你的小提琴代码中,你的函数在错误的范围内.尝试在 map 函数内移动函数,而不是在它自己的范围内......即,而不是:
in your fiddle code, your function is in the wrong scope. try moving the function inside the map function instead of in it's own scope... i.e. instead of:
});
function addMarker(e){
// Add marker to map at click location; add popup window
var newMarker = new L.marker(e.latlng).addTo(map);
}
使用
function addMarker(e){
// Add marker to map at click location; add popup window
var newMarker = new L.marker(e.latlng).addTo(map);
}
});
这篇关于如何使用传单 map.on('click', function) 事件处理程序将标记添加到地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用传单 map.on('click', function) 事件处理程序将标记添加到地图


基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01