Published
Edited
Jan 19, 2022
Insert cell
# Closest Points
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
map = {
const container = html`<div style="height:600px;">`;
let map = null
const attributionLayer = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "&copy; <a href=https://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors"
})
const reversedPivot = pivot.slice().reverse()

const pivotCircle = L.circle(reversedPivot)
const pivotMarker = L.marker(reversedPivot)

const redCircleIcon = L.icon({})

const targetMarkers = sortedDataset.map(d => L.marker([d.lat, d.long]))

container.update = function (radius) {
if(map) {
pivotCircle.setRadius(radius * 1000)
pivotCircle.addTo(map)
pivotMarker.addTo(map)
sortedDataset.forEach((d, i) => {
if (d.distance <= radius) {
targetMarkers[i].bindPopup(d.label).addTo(map)
} else {
targetMarkers[i].remove()
}
})
}
}

yield container;

map = L.map(container).setView(reversedPivot, 10)
attributionLayer.addTo(map)
}
Insert cell
Insert cell
map.update(radius)
Insert cell
dataset = file.text()
.then(text => d3.csvParse(text, d => ({
lat: d[fields.lat],
long: d[fields.long],
label: d[fields.label],
})))
Insert cell
decoratedDataset = dataset.map(d => Object.assign(d, {
distance: d3.geoDistance([d.long, d.lat], pivot) * EARTH_RADIUS_KM
}))
Insert cell
sortedDataset = decoratedDataset.slice().sort((a, b) => a.distance - b.distance)
Insert cell
EARTH_RADIUS_KM = 6371;
Insert cell
pivot = parsePoint(coordinates) || [13.237771192296224, 46.06596653564763]
Insert cell
Insert cell
html`<link href='${resolve('leaflet@1.2.0/dist/leaflet.css')}' rel='stylesheet' />`
Insert cell
L = await require("leaflet@1.2.0")
Insert cell
d3 = require('d3@7')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more