Directions Service
Compute routes between an origin and a destination.
Example
Directions Service
let map: woosmap.map.Map;
let fromSelect: HTMLSelectElement;
let toSelect: HTMLSelectElement;
function initMap(): void {
const directionsService = new woosmap.map.DirectionsService();
const directionsRenderer = new woosmap.map.DirectionsRenderer({});
map = new woosmap.map.Map(document.getElementById("map") as HTMLElement, {
zoom: 7,
center: {
lat: 43.5,
lng: 2.4,
},
});
directionsRenderer.setMap(map);
const onChangeHandler = (): void => {
calculateAndDisplayRoute(directionsService, directionsRenderer);
};
fromSelect = document.getElementById("from") as HTMLSelectElement;
toSelect = document.getElementById("to") as HTMLSelectElement;
fromSelect.addEventListener("change", onChangeHandler);
toSelect.addEventListener("change", onChangeHandler);
calculateAndDisplayRoute(directionsService, directionsRenderer);
}
function calculateAndDisplayRoute(
directionsService: woosmap.map.DirectionsService,
directionsRenderer: woosmap.map.DirectionsRenderer,
): void {
const displayMetadataRoute = (route: woosmap.map.DirectionRoute): void => {
if (route) {
(document.getElementById("distance") as HTMLElement).innerText =
route.legs[0].distance.text;
(document.getElementById("duration") as HTMLElement).innerText =
route.legs[0].duration.text;
}
};
directionsService.route(
{
origin: getSelectedLatLng(fromSelect),
destination: getSelectedLatLng(toSelect),
provideRouteAlternatives: true,
},
(response: woosmap.map.DirectionResult, status: string) => {
if (status === "OK") {
directionsRenderer.setDirections(response);
displayMetadataRoute(response.routes[0]);
directionsRenderer.addListener("routeIndex_changed", () => {
displayMetadataRoute(
response.routes[directionsRenderer.get("routeIndex")],
);
});
} else {
window.alert(`Directions request failed due to ${status}`);
}
},
);
}
function getSelectedLatLng(
selectElement: HTMLSelectElement,
): woosmap.map.LatLngLiteral {
return {
lat: parseFloat(
selectElement.options[selectElement.selectedIndex]?.getAttribute(
"data-lat",
) as string,
),
lng: parseFloat(
selectElement.options[selectElement.selectedIndex]?.getAttribute(
"data-lng",
) as string,
),
};
}
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
let map;
let fromSelect;
let toSelect;
function initMap() {
const directionsService = new woosmap.map.DirectionsService();
const directionsRenderer = new woosmap.map.DirectionsRenderer({});
map = new woosmap.map.Map(document.getElementById("map"), {
zoom: 7,
center: {
lat: 43.5,
lng: 2.4,
},
});
directionsRenderer.setMap(map);
const onChangeHandler = () => {
calculateAndDisplayRoute(directionsService, directionsRenderer);
};
fromSelect = document.getElementById("from");
toSelect = document.getElementById("to");
fromSelect.addEventListener("change", onChangeHandler);
toSelect.addEventListener("change", onChangeHandler);
calculateAndDisplayRoute(directionsService, directionsRenderer);
}
function calculateAndDisplayRoute(directionsService, directionsRenderer) {
const displayMetadataRoute = (route) => {
if (route) {
document.getElementById("distance").innerText =
route.legs[0].distance.text;
document.getElementById("duration").innerText =
route.legs[0].duration.text;
}
};
directionsService.route(
{
origin: getSelectedLatLng(fromSelect),
destination: getSelectedLatLng(toSelect),
provideRouteAlternatives: true,
},
(response, status) => {
if (status === "OK") {
directionsRenderer.setDirections(response);
displayMetadataRoute(response.routes[0]);
directionsRenderer.addListener("routeIndex_changed", () => {
displayMetadataRoute(
response.routes[directionsRenderer.get("routeIndex")],
);
});
} else {
window.alert(`Directions request failed due to ${status}`);
}
},
);
}
function getSelectedLatLng(selectElement) {
return {
lat: parseFloat(
selectElement.options[selectElement.selectedIndex]?.getAttribute(
"data-lat",
),
),
lng: parseFloat(
selectElement.options[selectElement.selectedIndex]?.getAttribute(
"data-lng",
),
),
};
}
window.initMap = initMap;
/*
* Always set the map height explicitly to define the size of the div element
* that contains the map.
*/
#map {
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;
}
#od-selector {
width: 200px;
position: absolute;
top: 0;
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;
}
.od-container {
padding: 2px 0;
width: 100%;
display: flex;
justify-content: space-between;
}
#od-selector span {
padding: 0 5px;
}
<html>
<head>
<title>Directions Service</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="od-selector">
<div class="od-container">
<span>from </span>
<select id="from">
<option value="Paris" data-lat="48.86288" data-lng="2.34946">
Paris
</option>
<option value="Montpellier" data-lat="43.61486" data-lng="3.86798">
Montpellier
</option>
<option value="Berlin" data-lat="52.52457" data-lng="13.42347">
Berlin
</option>
<option value="Milan" data-lat="45.47520" data-lng="9.18530">
Milan
</option>
<option value="Madrid" data-lat="40.42629" data-lng="-3.68574">
Madrid
</option>
<option value="Bruxelles" data-lat="50.84670" data-lng="4.35684">
Bruxelles
</option>
<option value="Amsterdam" data-lat="52.37342" data-lng="4.84631">
Amsterdam
</option>
</select>
</div>
<div class="od-container">
<span>to </span>
<select id="to">
<option value="Paris" data-lat="48.86288" data-lng="2.34946">
Paris
</option>
<option
value="Montpellier"
data-lat="43.61486"
data-lng="3.86798"
selected
>
Montpellier
</option>
<option value="Berlin" data-lat="52.52457" data-lng="13.42347">
Berlin
</option>
<option value="Milan" data-lat="45.47520" data-lng="9.18530">
Milan
</option>
<option value="Madrid" data-lat="40.42629" data-lng="-3.68574">
Madrid
</option>
<option value="Bruxelles" data-lat="50.84670" data-lng="4.35684">
Bruxelles
</option>
<option value="Amsterdam" data-lat="52.37342" data-lng="4.84631">
Amsterdam
</option>
</select>
</div>
<div class="od-container">
<span>distance </span>
<div id="distance"></div>
</div>
<div class="od-container">
<span>time </span>
<div id="duration"></div>
</div>
</div>
<!--The div element for the map -->
<div id="map"></div>
<script
src="https://sdk.woosmap.com/map/map.js?key=woos-48c80350-88aa-333e-835a-07f4b658a9a4&callback=initMap"
defer
></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/directions-simple 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
Was this article helpful?
Have more questions? Submit a request