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>
<div id="map"></div>
</div>
<script
src="https://sdk.woosmap.com/map/map.js?key=woos-e1e5490b-19d1-34f8-9955-fd5cfc8c880f&callback=initMap"
defer
></script>
</body>
</html>
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";
function initMap() {
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 = new woosmap.map.Map(document.getElementById("map"), {
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 marker = new woosmap.map.Marker({
position: userPosition,
icon: {
url: "https://images.woosmap.com/user-position-small.png",
},
});
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, ">");
return json.replace(
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\\-]?\d+)?)/g,
(match) => {
let cls = "number";
if...