Render Shapes - Polylines
Sample code for Woosmap Map JavaScript API
author(s):
Woosmap
HTML
<!DOCTYPE html>
<html>
<head>
<title>Render Shapes - Polylines</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 -->
<wmp-map id="map"></wmp-map>
<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: "woos-e1e5490b-19d1-34f8-9955-fd5cfc8c880f",
libraries: "",
language: "",
});
</script>
</body>
</html>
CSS
/*
* Always set the map height explicitly to define the size of the element
* that contains the map. `display: block` is required for <wmp-map>/<gmp-map>,
* which default to inline like any other custom element outside a flex/grid
* container (no-op for a plain <div>).
*/
#map {
display: block;
width: 100%;
height: 100%;
}
/*
* Optional: Makes the sample page fill the window.
*/
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;
}
JavaScript
// Initialize and add the map
let map;
async function initMap() {
await woosmap.map.importLibrary("maps");
map = document.getElementById("map").innerMap;
map.moveCamera({
center: { lat: 51.505, lng: -0.118 },
zoom: 15,
});
//Polyline Shape
const polylinePath = [
{ lng: -0.12638568878173828, lat: 51.50099581189912 },
{ lng: -0.12201905250549315, lat: 51.500832183172456 },
{ lng: -0.11891841888427736, lat: 51.50078877137083 },
{ lng: -0.11869311332702637, lat: 51.500832183172456 },
{ lng: -0.11754512786865233, lat: 51.500752038275635 },
{ lng: -0.11698186397552489, lat: 51.50050158456475 },
{ lng: -0.11669218540191649, lat: 51.50048488760175 },
{ lng: -0.11619329452514648, lat: 51.500688590132405 },
{ lng: -0.11590898036956787, lat: 51.50086557683785 },
{ lng: -0.1160377264022827, lat: 51.50112270724184 },
{ lng: -0.11700868606567383, lat: 51.50142992608211 },
{ lng: -0.11710524559020995, lat: 51.50164364231471 },
{ lng: -0.1157534122467041, lat: 51.50321309019227 },
{ lng: -0.11474490165710448, lat: 51.50420148069069 },
{ lng: -0.11411190032958984, lat: 51.50454874793857 },
{ lng: -0.11415, lat: 51.50545363381515 },
{ lng: -0.11816740036010741, lat: 51.50991769706187 },
];
//Polyline Object with Path as an Array of LatLng
const polyline = new woosmap.map.Polyline({
path: polylinePath,
strokeColor: "#b71c1c",
strokeOpacity: 0.8,
strokeWeight: 4,
});
polyline.setMap(map);
}
initMap();