map = {
const container = html`<div style="height:600px;">`;
yield container;
const map = L.map(container, {zoomSnap: 0}).setView([37.8, -96.9], 4);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© <a href=https://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors"
}).addTo(map);
const svg = d3.select(map.getPanes().overlayPane).append("svg").style("position", "relative"),
g = svg.append("g").attr("class", "leaflet-zoom-hide");
function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
const transform = d3.geoTransform({point: projectPoint}),
path = d3.geoPath().projection(transform);
var tip;
var feature = g.selectAll("path")
.data(states.features)
.enter().append("path")
.attr("d", path)
.style("fill", "#000")
.style("fill-opacity", .2)
.style("stroke", "#fff")
.style("stroke-width", "1.5px")
.style("pointer-events", "all")
.on("mouseover mousemove", function (event) {
d3.select(this).style("fill", "brown").style("fill-opacity", .7);
const pointer = d3.pointer(event);
const data = d3.select(this).data()[0];
tip.call(hover, pointer, data.properties.STATE.split("\n"))
})
.on("mouseout", function () {
d3.select(this).style("fill", "#000").style("fill-opacity", .2);
tip.call(hover, null);
});
tip = g.append("g")
.attr("transform", "translate(0, -10)")
.selectAll(".hover")
.data([""])
.join("g")
.attr("class", "hover")
.style("pointer-events", "none")
.style("text-anchor", "middle");
var bounds = path.bounds(states),
topLeft = bounds[0],
bottomRight = bounds[1];
svg.attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");
g.attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");
map.on("zoom", reset);
reset();
function reset() {
const bounds = path.bounds(states),
topLeft = bounds[0],
bottomRight = bounds[1];
svg .attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");
g .attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");
feature.attr("d", path);
}
}