I'm trying to create an interactive map using the Leaflet.FileLayer
plugin. This plugin allows me to load geojson files with description but I don't know how to show this description in Popup when data is loaded from separate file. Normally, I call data using this code:
var miejsca_pamieci_l = L.geoJson(miejsca_pamieci, {
pointToLayer: function (feature, latLng) {
return new L.Marker(latLng, {
icon: new myIcon({
iconUrl: 'icon/cemetery-24.png',
iconSize: [24, 24],
iconAnchor: [12, 24],
popupAnchor: [0, -24]
})
})
},
onEachFeature: function( feature, layer ){
layer.bindPopup (feature.properties.Opis, { maxWidth: "auto"})
} });
But how to change the code to show popup using FileLayer
? This is my script in html code:
(function (window) {
'use strict';
function initMap() {
var L = window.L;
var osm = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © 2013 OpenStreetMap contributors'
});
var map = L.map('map', {
center: [0, 0],
zoom: 2
}).addLayer(osm);
var style = {
color: 'red',
opacity: 1.0,
fillOpacity: 1.0,
weight: 2,
clickable: false
};
L.Control.FileLayerLoad.LABEL = '';
L.Control.fileLayerLoad({
fitBounds: true,
layerOptions: {
style: style,
pointToLayer: function (data, latlng) {
return L.circleMarker(
latlng,
{ style: style }
);
}
}
}).addTo(map);
}
window.addEventListener('load', function () {
initMap();
});
}(window));
Best Answer
To make your popups with this plugin you have to do the following:
Make sure your loaded geojson has
"popupContent"
in the feature properties, e.g.Of course you can also load values from other properties, but then remember to change the call to it in
onEachFeature
function.Set your layer style to
clickable: true
.Modify a little bit your L.Control.fileLayerLoad to make it a
layer
variable and add thereonEachFeature
function.Finally add the onEachFeature function:
Below you have the whole working example. LeafletJS scripts are loaded from CDNs, leaflet.filelayer.js is loaded from local, so while testing make sure you change it to your path.
You can also check out this codepen to play around: http://codepen.io/dagmara223/pen/EZdZvp
pozdrowienia :)