4

I have a problem with my geojson layers. When I lunch my site they are all switched on, but I don't want to. Is there any solution to have lunched only one layer when my site starts, not the all one? Here is the simple example what I want https://jsfiddle.net/expedio/dtLyvffg/

And this is the code with my layers:

var controlLayers = L.control.layers().addTo(map);

$.getJSON("data/Zabudowa.geoJSON", function(geojson) {
  var geojsonLayer = L.geoJson(geojson, {
    style: function(feature) {
      return {
        'weight': 0,
        'fillColor': 'brown',
        'fillOpacity': 1
      }
    },
    onEachFeature: function(feature, layer) {
      var popupText = "Numer obiektu: " + feature.properties.OBJECTID + "<br>Sposob uzytkowania: " + feature.properties.type + "<br><a href='" + feature.properties.url + "'>More info</a>";
      layer.bindPopup(popupText);
    }
  }).addTo(map);
  controlLayers.addOverlay(geojsonLayer, 'Zabudowa');
});


controlLayers.addOverlay(geojsonLayer, 'Drogi');
});

var myIcon = L.Icon.extend({
  iconUrl: 'icons/cemetery-24.png'

});
$.getJSON("data/miejsca_pamieci.geoJSON", function(geojson) {
  var geojsonLayer = L.geoJson(geojson, {
    pointToLayer: function(feature, latLng) {
      return new L.Marker(latLng, {
        icon: new myIcon({
          iconUrl: 'icons/cemetery-24.png',
          iconSize: [24, 24],
          iconAnchor: [12, 24],
          popupAnchor: [0, -24]
        })
      })
    },
    onEachFeature: function(feature, layer) {
      layer.bindPopup("Opis: " + feature.properties.Opis)
    }
  }).addTo(map);
  controlLayers.addOverlay(geojsonLayer, 'Miejsca_pamieci');
});

var myIcon = L.Icon.extend({
  iconUrl: 'icons/accessdenied.png'

});

$.getJSON("data/Fotografie.geoJSON", function(geojson) {
  var geojsonLayer = L.geoJson(geojson, {
    pointToLayer: function(feature, latLng) {
      return new L.Marker(latLng, {
        icon: new myIcon({
          iconUrl: 'icons/accessdenied.png',
          iconSize: [32, 37],
          iconAnchor: [16, 37],
          popupAnchor: [0, -37]
        })
      })
    },
    onEachFeature: function(feature, layer) {
      layer.bindPopup(feature.properties.Name, {
        maxWidth: "auto"
      })
    }
  }).addTo(map);
  controlLayers.addOverlay(geojsonLayer, 'Fotografie');
});
3

Simply do not add your layers to the map (remove the .addTo(map) after your L.geoJson() factories), but only to your controlLayers Layers Control.

  • Thanks! It was so simple that I couldn't even imagine. It's becouse I'm green in javascripts and all the stuff but I'm learning every day something new. – bass Aug 21 '16 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.