Leaflet eachLayer function does not iterate through all Layers

一曲冷凌霜 提交于 2020-01-15 05:53:09

问题


Created some markers using an array of GeoJSON data:

$.getJSON("GetLocationsServlet", function(data) {
    L.geoJSON(data, {
        onEachFeature: onEachFeature
    }).addTo(mymap);
});

The GeoJSON data is like this:

[
{ "type": "Feature", "properties": { "name": "Riverway Sport Complex", "amenity": "GYM", "popupContent": "Riverway Sport Complex" }, "geometry": { "type": "Point", "coordinates": [-123.002846, 49.205036] },"id" : "1"} ,
{ "type": "Feature", "properties": { "name": "Imperial@Patterson", "amenity": "GYM", "popupContent": "Imperial@Patterson" }, "geometry": { "type": "Point", "coordinates": [-123.01249, 49.22193] },"id" : "2"} 
]

The markers were successfully created and showed on the map. At some point later, I needed to iterate through all Markers, so I used eachLayer function:

    var mymap = L.map('mapid').locate({setView: true, maxZoom: 15});

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png? '
    access_token=..., {
    maxZoom: 25,
    attribution: 
    id: 'mapbox.streets',
    }).addTo(mymap);

    .......
    $.getJSON( L.geoJSON()... )        
    .......

    mymap.eachLayer(function(layer) {
        alert (layer.options.id); 
       // Above alert successfully print out the tileLayer ID
        if (layer instanceof L.Marker) {
            alert("Marker [" + layer.options.title + "]");
        }
    });

However, it only looped through the main map tileLayer and stopped. Am I using the eachLayer method correctly? Marker is also a subclass of Layer?

Thanks,

来源:https://stackoverflow.com/questions/50354728/leaflet-eachlayer-function-does-not-iterate-through-all-layers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!