我已经开始使用传单作为开源 map ,http://leaflet.cloudmade.com/

以下jQuery代码将允许在 map 单击时在 map 上创建标记:

 map.on('click', onMapClick);
function onMapClick(e) {
        var marker = new L.Marker(e.latlng, {draggable:true});
        map.addLayer(marker);
        marker.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
};

But there is currently no way for me (in my code) to delete existing markers, or find all the markers i've created on a map and put them into an array. Can anyone help me understand how to do this? Leaflet documentation is available here : http://leaflet.cloudmade.com/reference.html

推荐答案

您必须将"var标记"从函数中删除.然后,稍后您可以访问它:

var marker;
function onMapClick(e) {
        marker = new L.Marker(e.latlng, {draggable:true});
        map.addLayer(marker);
        marker.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
};

然后稍后:

map.removeLayer(marker)

But you can only have the latest marker that way, because each time, the var marker is erased by the latest. So one way to go is to create a global array of marker, and you add your marker in the global array.

Jquery相关问答推荐

使用动态类名的gm_addStyle?

点击和touch 事件之间的jQuery冲突解决方法

jQuery在页面加载 timeshift 动到锚点位置

在 jQuery 中构建 html 元素的最清晰方法

在页面上放置文件时如何设置文件输入值?

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

找到下一个匹配的sibling 姐妹的有效,简洁的方法?

jQuery UI 对话框 - 关闭后不打开

不使用插件的 jQuery 缓动函数

在单击事件上判断 Ctrl / Shift / Alt 键

jQuery计算所有文本字段中的值的总和

AngularJS + JQuery:如何在 angularjs 中获取动态内容

jquery 从特定表单获取所有输入

未捕获的 TypeError:data.push 不是函数

jQuery 日期 Select 器 - 禁用过go 的日期

如何使用 jQuery 或纯 JS 重置所有复选框?

如何使用 jQuery 删除 cookie?

带有回调ajax json的jQuery自动完成

通过 AJAX MVC 下载 Excel 文件

你能在不影响历史的情况下使用哈希导航吗?