Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
31 lines (31 loc) · 1.42 KB
/
Copy pathexample.js
File metadata and controls
31 lines (31 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
let map;
document.querySelector("#example").addEventListener("submit", async (event) => {
event.preventDefault();
const form = event.currentTarget;
const button = form.querySelector("button");
const status = document.querySelector("#status");
const values = new FormData(form);
const key = String(values.get("key")).trim();
button.disabled = true;
status.textContent = "Loading…";
try {
if (!globalThis.unmap) throw new Error("The browser SDK could not load. Check your connection.");
if (document.body.dataset.example === "basic-map") {
map?.remove();
map = new unmap.Unmap({ key, container: "#map", center: [-114.07, 51.05], zoom: 12 }).map;
map.on("load", () => { status.textContent = "Map loaded."; });
map.on("error", (event) => { status.textContent = event.error?.message || "The map request failed."; });
} else {
const client = new unmap.Unmap({ key });
const result = document.body.dataset.example === "geocoding"
? await client.geocoder.search(String(values.get("query")))
: await client.router.route([-114.0631, 51.0443], [-114.0703, 51.0552], { mode: "pedestrian" });
document.querySelector("#result").textContent = JSON.stringify(result, null, 2);
status.textContent = "Request complete.";
}
} catch (error) {
status.textContent = error instanceof Error ? error.message : String(error);
} finally {
button.disabled = false;
}
});