Public
Edited
Jan 17, 2023
2 forks
Importers
5 stars
Insert cell
Insert cell
Insert cell
viewof coordinates = getUserCoordinates({})
Insert cell
coordinates
Insert cell
globe = Plot.marks([
Plot.sphere({ fill: "#64b5f6" }),
Plot.graticule({ stroke: "#fff" }),
Plot.geo(land, { fill: "#bbdefb" }),
Plot.geo(createGeoJsonPoint(coordinates), { fill: "red" })
]).plot({ width, projection: { type: "equirectangular" } })
Insert cell
Insert cell
Insert cell
Insert cell
function getUserCoordinates(options) {
const { buttonText, auto, ...geolocationOptions } = {
buttonText: "Get Location",
enableHighAccuracy: false,
auto: true,
...options
};

const form = html`<form style="${styles.form}"></form>`;
const label = html`<label style="${styles.label}">↑ Allow location access</label>`;
const button = html`<button>${buttonText}</button>`;

// We show the coordinates and enable the button once we obtain
// the user coordinates.
function onSuccess(position) {
form.value = getCoordinates(position);
label.innerText = `Lat: ${position.coords.latitude}, Lon: ${position.coords.longitude}`;
label.style = styles.label;
button.disabled = false;
form.dispatchEvent(new Event("input"));
}

// Show an an error message if we can't obtain the coordinates
// and re-enable the button, so the user can try again.
function onError(err) {
label.innerText = `Error: ${err.message}`;
label.style = styles.labelError;
button.disabled = false;
}

button.onclick = () => {
navigator.geolocation.getCurrentPosition(
onSuccess,
onError,
geolocationOptions
);
label.innerText = "Loading...";
label.style = styles.label;
button.disabled = true;
};

if (auto) {
const promise = new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
onSuccess(position);
resolve(position.coords);
},
(error) => {
onError(error);
reject(error);
}
);
}).catch((error) => error);
}

form.value = {};
form.addEventListener("submit", (e) => e.preventDefault());
form.append(label, button);
return form;
}
Insert cell
Insert cell
Insert cell
Insert cell
import { land } from "@observablehq/plot-projections"
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