function* map(geojson, options = {}) {
if (!options.tileURL)
options.tileURL = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
document.head.appendChild(
html`<link id=leaflet-sheet href='${resolve(
"leaflet@1.7.1/dist/leaflet.css"
)}' rel='stylesheet' />`
);
const displayWidth = options.width || width;
const displayHeight = options.height || displayWidth / 1.6;
let container = html`<div style='width:${displayWidth}px;height:${displayHeight}px'></div>`;
yield container;
let map = L.map(container);
if (geojson) {
if (Array.isArray(geojson)) {
geojson = { type: "FeatureCollection", features: geojson };
}
function onEachFeature(feature, layer) {
// options
if (feature.properties) {
if (options.stringifyProps) {
layer.bindPopup(
html`<pre style='max-width:200px;max-height:300px;overflow:auto'>${JSON.stringify(
feature.properties,
null,
2
)}</pre>`
);
} else {
const popupString = Object.keys(feature.properties).reduce(
(str, prop) => {
let value = feature.properties[prop];
if (typeof feature.properties[prop] === "object") {
value = JSON.stringify(feature.properties[prop], null, 2);
}
return (str += `${prop}: ${value}\n`);
},
""
);
layer.bindPopup(
html`<pre style='max-width:200px;max-height:300px;overflow:auto'>${popupString}</pre>`
);
}
}
}
function style(feature) {
if (feature.properties) {
return feature.properties.style;
}
}
let gj = L.geoJson(geojson, {
onEachFeature,
style
}).addTo(map);
// gj.eachLayer;
map.fitBounds(gj.getBounds());
if (
!geojson.features ||
(geojson.features && geojson.features.length === 1)
) {
map.eachLayer(function (layer) {
layer.openPopup();
});
}
} else {
const center = options.center || [0, 0];
const zoom = options.zoom || 3;
map.setView(center, zoom);
}
container.value = map;
let osmLayer = L.tileLayer(options.tileURL, {
attribution:
options.attribution ||
'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}