Distance Isochrone
Distance Isochrones lets you calculate distance isochrones from a given location.
Example
Distance Isochrone
let map: woosmap.map.Map;
let methodSelect, modeSelect: HTMLSelectElement;
let distanceInput: HTMLInputElement;
let marker: woosmap.map.marker.AdvancedMarkerElement;
let polyline: woosmap.map.Polyline;
let distanceService: woosmap.map.DistanceService;
const centerLatLng: woosmap.map.LatLngLiteral = {
lat: 51.5,
lng: -0.234,
};
function createMarkerContent(): HTMLElement {
return new woosmap.map.marker.PinElement().element;
}
function createMarker(
position: woosmap.map.LatLngLiteral,
map: woosmap.map.Map,
) {
const marker = new woosmap.map.marker.AdvancedMarkerElement({
position,
content: createMarkerContent(),
});
marker.setMap(map);
return marker;
}
async function initMap() {
await woosmap.map.importLibrary("maps");
await woosmap.map.importLibrary("marker");
map = (document.getElementById("map") as woosmap.map.MapElement).innerMap;
map.moveCamera({
zoom: 9,
center: centerLatLng,
});
distanceService = new woosmap.map.DistanceService();
marker = createMarker(centerLatLng, map);
map.addListener("click", (e) => {
marker.setMap(null);
marker = createMarker(e.latlng, map);
computeIsochrone(e.latlng);
});
const onChangeHandler = () => {
computeIsochrone(marker.position!.toJSON());
};
methodSelect = document.getElementById(
"isochrone-method-select",
) as HTMLSelectElement;
modeSelect = document.getElementById(
"isochrone-mode-select",
) as HTMLSelectElement;
distanceInput = document.getElementById(
"isochrone-distance-input",
) as HTMLInputElement;
methodSelect.addEventListener("change", onChangeHandler);
modeSelect.addEventListener("change", onChangeHandler);
distanceInput.addEventListener("change", onChangeHandler);
computeIsochrone(centerLatLng);
}
function computeIsochrone(
origin: woosmap.map.LatLng | woosmap.map.LatLngLiteral,
) {
if (
parseInt(distanceInput.value) > 120 ||
parseInt(distanceInput.value) <= 0
) {
alert("Value should be between 1 and 120");
return;
}
const isochroneRequest: woosmap.map.distance.DistanceIsochroneRequest = {
origin,
method: methodSelect.value,
travelMode: modeSelect.value,
value: parseInt(distanceInput.value),
};
distanceService
.getDistanceIsochrone(isochroneRequest)
.then(displayIsochrone)
.catch((error) => {
console.error(error);
});
}
function displayIsochrone(
isochrone: woosmap.map.distance.DistanceIsochroneResponse,
) {
if (polyline) {
polyline.setMap(null);
}
polyline = new woosmap.map.Polyline({
path: isochrone.isoline.path || null,
strokeColor: "#b71c1c",
strokeOpacity: 0.8,
strokeWeight: 4,
});
polyline.setMap(map);
fitToCoordinates(isochrone.isoline.path || null);
}
function fitToCoordinates(
coordinates: woosmap.map.LatLng[] | woosmap.map.LatLngLiteral[] | null,
) {
const bounds = new woosmap.map.LatLngBounds();
if (coordinates) {
for (const latlng of coordinates) {
bounds.extend(latlng);
}
map.fitBounds(bounds);
}
}
initMap();
let map;
let methodSelect, modeSelect;
let distanceInput;
let marker;
let polyline;
let distanceService;
const centerLatLng = {
lat: 51.5,
lng: -0.234,
};
function createMarkerContent() {
return new woosmap.map.marker.PinElement().element;
}
function createMarker(position, map) {
const marker = new woosmap.map.marker.AdvancedMarkerElement({
position,
content: createMarkerContent(),
});
marker.setMap(map);
return marker;
}
async function initMap() {
await woosmap.map.importLibrary("maps");
await woosmap.map.importLibrary("marker");
map = document.getElementById("map").innerMap;
map.moveCamera({
zoom: 9,
center: centerLatLng,
});
distanceService = new woosmap.map.DistanceService();
marker = createMarker(centerLatLng, map);
map.addListener("click", (e) => {
marker.setMap(null);
marker = createMarker(e.latlng, map);
computeIsochrone(e.latlng);
});
const onChangeHandler = () => {
computeIsochrone(marker.position.toJSON());
};
methodSelect = document.getElementById("isochrone-method-select");
modeSelect = document.getElementById("isochrone-mode-select");
distanceInput = document.getElementById("isochrone-distance-input");
methodSelect.addEventListener("change", onChangeHandler);
modeSelect.addEventListener("change", onChangeHandler);
distanceInput.addEventListener("change", onChangeHandler);
computeIsochrone(centerLatLng);
}
function computeIsochrone(origin) {
if (
parseInt(distanceInput.value) > 120 ||
parseInt(distanceInput.value) <= 0
) {
alert("Value should be between 1 and 120");
return;
}
const isochroneRequest = {
origin,
method: methodSelect.value,
travelMode: modeSelect.value,
value: parseInt(distanceInput.value),
};
distanceService
.getDistanceIsochrone(isochroneRequest)
.then(displayIsochrone)
.catch((error) => {
console.error(error);
});
}
function displayIsochrone(isochrone) {
if (polyline) {
polyline.setMap(null);
}
polyline = new woosmap.map.Polyline({
path: isochrone.isoline.path || null,
strokeColor: "#b71c1c",
strokeOpacity: 0.8,
strokeWeight: 4,
});
polyline.setMap(map);
fitToCoordinates(isochrone.isoline.path || null);
}
function fitToCoordinates(coordinates) {
const bounds = new woosmap.map.LatLngBounds();
if (coordinates) {
for (const latlng of coordinates) {
bounds.extend(latlng);
}
map.fitBounds(bounds);
}
}
initMap();
/*
* 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;
}
#app {
height: 100%;
}
#selector {
position: absolute;
top: 0px;
left: 0;
background-color: #fff;
border-radius: 3px;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
margin: 10px;
padding: 5px;
overflow: hidden;
z-index: 1;
}
select {
width: 100px;
}
input {
width: 92px;
}
.select-container {
padding: 2px 0;
width: 100%;
display: flex;
justify-content: space-between;
}
#selector span {
padding: 0 5px;
}
<html>
<head>
<title>Distance Isochrone</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="app">
<div id="selector">
<div class="select-container">
<span>mode </span>
<select id="isochrone-mode-select">
<option value="driving">Driving</option>
<option value="walking">Walking</option>
<option value="cycling">Cycling</option>
</select>
</div>
<div class="select-container">
<span>method </span>
<select id="isochrone-method-select">
<option value="time">Time</option>
<option value="distance">Distance</option>
</select>
</div>
<div class="select-container">
<span>value </span>
<input
id="isochrone-distance-input"
type="number"
value="50"
min="1"
max="120"
/>
</div>
<div class="select-container">
<small style="opacity: 0.5; padding: 0 5px"
>click on the map to change isochrone origin</small
>
</div>
</div>
<wmp-map id="map"></wmp-map>
</div>
<script>
(g=>{var p,w=window,d=document,l="importLibrary",q="__ib__",e="The Woosmap Maps JavaScript API",o=w.woosmap||(w.woosmap={}),m=o.map||(o.map={}),r=new Set(String(g.libraries||"").split(",").filter(Boolean)),s=new URLSearchParams,u=()=>p||(p=new Promise((f,n)=>{var a=d.createElement("script");s.set("libraries",[...r]+"");for(var k in g)k!="libraries"&&g[k]&&s.set(k,g[k]);s.set("callback","woosmap.map."+q);a.src="https://sdk.woosmap.com/map/map.js?"+s;m[q]=f;a.onerror=()=>n(Error(e+" could not load."));a.nonce=d.querySelector("script[nonce]")?.nonce||"";d.head.append(a)}));m[l]?console.warn(e+" only loads once. Ignoring:",g):m[l]=(f,...n)=>r.add(f)&&u().then(()=>m[l](f,...n))})({
key: "woos-e1e5490b-19d1-34f8-9955-fd5cfc8c880f",
libraries: "marker",
});
</script>
</body>
</html>
Running the Sample Locally
Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow these instructions to get them set up.
Once you have Git and Node.js installed, you can run the sample by following these steps:
-
Clone the repository and navigate to the directory of the sample.
-
Install the necessary dependencies.
-
Start running the sample.
Here are the commands you can use in your terminal to do this:
git clone -b sample/distance-isochrone https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start
You can experiment with other samples by switching to any branch that starts with the pattern sample/SAMPLE_NAME.
git checkout sample/SAMPLE_NAME
npm i
npm start