去除小叶层和L.marker方法

Mr.*_*ato 16 javascript leaflet

我想知道是否有人知道在使用此约定添加它们之后如何或是否可以实际删除一层点:

var pointsLayer, someFeatures = [{
            //Hard coded for now
            "type": "Feature",
            "properties": {
                "name": "Company A",
                "show_on_map": true,
                "icon": 'img/violations.png'
            },
            "geometry": {
                "type": "Point",
                "coordinates": [43.22519, -107.69348]
            }
        }, {
            "type": "Feature",
           .
           .
           .
   }];
for(w=0; w < someFeatures.length; w++){
                pointsLayer = L.marker(someFeatures[w].geometry.coordinates, {icon: violations})   
                    .bindPopup("Company: "+someFeatures[w].properties.name);
                    //add map points 
                    map.addLayer(pointsLayer);
            }
Run Code Online (Sandbox Code Playgroud)

典型的removeLayer(pointsLayer); 在类似的for循环中对我不起作用.但是,这并不意味着没有办法循环.我只是不确定如何.我正在尝试添加有效的点,然后在事件中删除它们(不工作).有任何想法吗?

谢谢大家.

PS如果您认为这个问题相关或有帮助,那么请不要忘了竖起大拇指,欢呼.

mel*_*elc 40

您可以在单独的图层(即var markers = new L.FeatureGroup();)上添加标记,然后map.addLayer(markers);在循环外的地图()上添加该图层,而不是直接在地图上添加所有标记.

例如,

http://jsfiddle.net/9BXL7/

HTML

<button>remove all markers</button>
<div id="map"></div>
Run Code Online (Sandbox Code Playgroud)

CSS

html, body, #map {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}
Run Code Online (Sandbox Code Playgroud)

JS

var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/997/256/{z}/{x}/{y}.png', {
    maxZoom: 18,
    attribution: 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
    key: 'BC9A493B41014CAABB98F0471D759707'
});

var map = L.map('map')
    .setView([50.5, 30.51], 15)
    .addLayer(cloudmade);

var markers = new L.FeatureGroup();

function getRandomLatLng(map) {
    var bounds = map.getBounds(),
        southWest = bounds.getSouthWest(),
        northEast = bounds.getNorthEast(),
        lngSpan = northEast.lng - southWest.lng,
        latSpan = northEast.lat - southWest.lat;

    return new L.LatLng(
    southWest.lat + latSpan * Math.random(),
    southWest.lng + lngSpan * Math.random());
}

function populate() {
    for (var i = 0; i < 10; i++) {
        var marker = L.marker(getRandomLatLng(map));
        marker.bindPopup("<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque.</p>", {
            showOnMouseOver: true
        });
        markers.addLayer(marker);
    }
    return false;
}

map.addLayer(markers);

populate();
function removeAllMarkers(){
    map.removeLayer(markers);
}
document.querySelector('button').onclick=function(){removeAllMarkers()};
Run Code Online (Sandbox Code Playgroud)

您是否需要删除或清除标记图层以替换将来使用的点:

markers.clearLayers();
Run Code Online (Sandbox Code Playgroud)


f1l*_*t3r 12

用途map.removeLayer():

var circle = L.circle([lat, lng], 1000).addTo(map);
map.removeLayer(circle);
Run Code Online (Sandbox Code Playgroud)

  • 我不会将其归类为"仅代码",因为"使用map.removeLayer:我认为重要的方式不同,是消化的信息较少.我正在为那些人添加它.正在扫描而不是真正详细地阅读事物...所以将_more information_添加到我故意尝试离开稀疏的东西本来就很奇怪. (2认同)