Geolocation Api
Sample code for Woosmap Map JavaScript API
author(s):
Woosmap
HTML
<!DOCTYPE html>
<html>
<head>
<title>Geolocation Api</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<!-- jsFiddle will insert css and js -->
</head>
<body>
<!--The div element for the map -->
<div id="container">
<div id="sidebar">
<strong>Geolocation API Response</strong>
<pre id="response"></pre>
</div>
<wmp-map id="map"></wmp-map>
</div>
<script>
((g) => {
let p;
const w = window,
d = document;
const L = "importLibrary",
CB = "__ib__";
const E = "The Woosmap Maps JavaScript API";
const woosmap = w.woosmap || (w.woosmap = {});
const maps = woosmap.map || (woosmap.map = {});
// Seed with the sample's configured libraries: some (e.g. widgets) are
// not importLibrary() names and only load through the script URL.
const libs = new Set(
String(g.libraries || "")
.split(",")
.filter(Boolean),
),
params = new URLSearchParams();
const load = () =>
p ||
(p = new Promise((res, rej) => {
const s = d.createElement("script");
params.set("libraries", [...libs].join(","));
for (const k in g)
if (k !== "libraries" && g[k]) params.set(k, g[k]);
params.set("callback", "woosmap.map." + CB);
s.src = "https://sdk.woosmap.com/map/map.js?" + params;
maps[CB] = res;
s.onerror = () => rej(Error(E + " could not load."));
s.nonce = d.querySelector("script[nonce]")?.nonce || "";
d.head.append(s);
}));
if (maps[L]) {
console.warn(E + " only loads once. Ignoring:", g);
} else {
maps[L] = (name, ...args) =>
libs.add(name) && load().then(() => maps[L](name, ...args));
}
})({
key:...
CSS
html,
body {
height: 100%;
margin: 0;
padding: 0;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}
#container {
height: 100%;
display: flex;
}
#sidebar {
flex-basis: 12rem;
flex-grow: 1;
max-width: 30rem;
height: 100%;
box-sizing: border-box;
overflow: auto;
}
#map {
flex-basis: 70vw;
flex-grow: 5;
height: 100%;
}
#sidebar {
padding: 5px;
flex-basis: 25rem;
}
pre .string {
color: green;
}
pre .number {
color: magenta;
}
pre .boolean {
color: blue;
}
pre .null {
color: magenta;
}
pre .key {
color: #1D1D1D;
}
JavaScript
let map;
const woosmap_key = "YOUR_API_KEY";
async function initMap() {
await woosmap.map.importLibrary("maps");
await woosmap.map.importLibrary("marker");
fetch(`https://api.woosmap.com/geolocation/position/?key=${woosmap_key}`)
.then((result) => {
return result.json();
})
.then((position) => {
renderUserLocationOnMap(position);
displayGeolocationResponse(position);
});
}
function renderUserLocationOnMap({ latitude, accuracy, longitude, viewport }) {
const userPosition = {
lat: latitude,
lng: longitude,
};
let zoom = 12;
if (accuracy > 200) {
zoom = 4;
} else if (accuracy > 50) {
zoom = 5;
} else if (accuracy > 25) {
zoom = 8;
} else if (accuracy > 10) {
zoom = 9;
} else if (accuracy > 5) {
zoom = 10;
} else if (accuracy > 1) {
zoom = 11;
}
map = document.getElementById("map").innerMap;
map.moveCamera({ center: userPosition, zoom });
if (viewport) {
const bounds = {
east: viewport.northeast.lng,
south: viewport.southwest.lat,
north: viewport.northeast.lat,
west: viewport.southwest.lng,
};
map.fitBounds(bounds);
plotBoundary(bounds);
}
const markerContent = document.createElement("img");
markerContent.src = "https://images.woosmap.com/user-position-small.png";
const marker = new woosmap.map.marker.AdvancedMarkerElement({
position: userPosition,
content: markerContent,
});
marker.setMap(map);
}
function displayGeolocationResponse(position) {
const html = [];
html.push(syntaxHighlight(position));
const $infoContainer = document.getElementById("response");
if ($infoContainer) {
$infoContainer.innerHTML = html.join("");
$infoContainer.style.display = "block";
}
}
function syntaxHighlight(json) {
if (typeof json != "string") {
json = JSON.stringify(json, undefined, 2);
}
json = json
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
...