Distance Matrix
Using the DistanceService to calculate distances and durations between multiple origins and destinations
Example
Distance Matrix
let map: woosmap.map.Map;
let bounds: woosmap.map.LatLngBounds;
let distanceService: woosmap.map.DistanceService;
let distanceRequest: woosmap.map.distance.DistanceMatrixRequest;
const markersArray: woosmap.map.Marker[] = [];
function createMarker(
position: woosmap.map.LatLng,
label: string,
url: string,
): woosmap.map.Marker {
return new woosmap.map.Marker({
map,
position,
icon: {
url,
labelOrigin: new woosmap.map.Point(14, 15),
scaledSize: {
height: 38,
width: 26,
},
},
label: {
text: label,
color: "white",
},
});
}
function fitBoundsToMatrix(
origins: woosmap.map.LatLng[] | woosmap.map.LatLngLiteral[],
destinations: woosmap.map.LatLng[] | woosmap.map.LatLngLiteral[],
): void {
origins.forEach((origin) => {
bounds.extend(origin);
markersArray.push(
createMarker(origin, "O", "https://images.woosmap.com/marker-blue.svg"),
);
});
destinations.forEach((destination) => {
bounds.extend(destination);
markersArray.push(
createMarker(
destination,
"D",
"https://images.woosmap.com/marker-red.svg",
),
);
});
map.fitBounds(bounds, { top: 70, bottom: 40, left: 50, right: 50 }, true);
}
function createRequest(): woosmap.map.distance.DistanceMatrixRequest {
const origin1: woosmap.map.LatLngLiteral = { lat: 45.4642, lng: 9.19 };
const origin2: woosmap.map.LatLngLiteral = { lat: 45.75, lng: 4.85 };
const destinationA: woosmap.map.LatLngLiteral = { lat: 42.6976, lng: 9.45 };
const destinationB: woosmap.map.LatLngLiteral = {
lat: 41.9028,
lng: 12.4964,
};
return {
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: woosmap.map.TravelMode.DRIVING,
unitSystem: woosmap.map.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false,
};
}
function handleResponse(
response: woosmap.map.distance.DistanceMatrixResponse,
): void {
const responseElement = document.getElementById("response");
if (responseElement) {
responseElement.innerText = JSON.stringify(response, null, 2);
fitBoundsToMatrix(distanceRequest.origins, distanceRequest.destinations);
}
}
function calculateDistances(): void {
distanceRequest = createRequest();
const requestElement = document.getElementById("request");
if (requestElement) {
requestElement.innerText = JSON.stringify(distanceRequest, null, 2);
distanceService.getDistanceMatrix(distanceRequest).then(handleResponse);
}
}
function initMap(): void {
map = new woosmap.map.Map(document.getElementById("map") as HTMLElement, {
center: { lat: 45.53, lng: 2.4 },
zoom: 10,
});
distanceService = new woosmap.map.DistanceService();
bounds = new woosmap.map.LatLngBounds();
calculateDistances();
}
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
let map;
let bounds;
let distanceService;
let distanceRequest;
const markersArray = [];
function createMarker(position, label, url) {
return new woosmap.map.Marker({
map,
position,
icon: {
url,
labelOrigin: new woosmap.map.Point(14, 15),
scaledSize: {
height: 38,
width: 26,
},
},
label: {
text: label,
color: "white",
},
});
}
function fitBoundsToMatrix(origins, destinations) {
origins.forEach((origin) => {
bounds.extend(origin);
markersArray.push(
createMarker(origin, "O", "https://images.woosmap.com/marker-blue.svg"),
);
});
destinations.forEach((destination) => {
bounds.extend(destination);
markersArray.push(
createMarker(
destination,
"D",
"https://images.woosmap.com/marker-red.svg",
),
);
});
map.fitBounds(bounds, { top: 70, bottom: 40, left: 50, right: 50 }, true);
}
function createRequest() {
const origin1 = { lat: 45.4642, lng: 9.19 };
const origin2 = { lat: 45.75, lng: 4.85 };
const destinationA = { lat: 42.6976, lng: 9.45 };
const destinationB = {
lat: 41.9028,
lng: 12.4964,
};
return {
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: woosmap.map.TravelMode.DRIVING,
unitSystem: woosmap.map.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false,
};
}
function handleResponse(response) {
const responseElement = document.getElementById("response");
if (responseElement) {
responseElement.innerText = JSON.stringify(response, null, 2);
fitBoundsToMatrix(distanceRequest.origins, distanceRequest.destinations);
}
}
function calculateDistances() {
distanceRequest = createRequest();
const requestElement = document.getElementById("request");
if (requestElement) {
requestElement.innerText = JSON.stringify(distanceRequest, null, 2);
distanceService.getDistanceMatrix(distanceRequest).then(handleResponse);
}
}
function initMap() {
map = new woosmap.map.Map(document.getElementById("map"), {
center: { lat: 45.53, lng: 2.4 },
zoom: 10,
});
distanceService = new woosmap.map.DistanceService();
bounds = new woosmap.map.LatLngBounds();
calculateDistances();
}
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;
}
#app {
height: 100%;
display: flex;
flex-direction: column;
}
#map,
#service-info {
height: 50%;
width: 100%;
}
h4 {
margin-bottom: 0px;
padding-bottom: 5px;
border-bottom: 1px solid #dfdfdf;
}
#service-info {
display: flex;
overflow-y: auto;
}
#request,
#response {
width: 100%;
}
<html>
<head>
<title>Distance Matrix</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="map"></div>
<div id="service-info">
<div style="width: 50%; margin: 5px">
<h4>Request</h4>
<pre id="request"></pre>
</div>
<div style="width: 50%; margin: 5px">
<h4>Response</h4>
<pre id="response"></pre>
</div>
</div>
</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/distance-matrix 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