Distance Matrix - Advanced

Sample code for Woosmap Map JavaScript API author(s): Woosmap

HTML

<!DOCTYPE html>
<html>
  <head>
    <title>Distance Matrix - Advanced</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />

    <!-- jsFiddle will insert css and js -->
  </head>
  <body>
    <div id="container">
      <div id="sidebar">
        <div id="innerWrapper">
          <div class="sectionHeader"><span>Travel mode</span></div>
          <div id="travelModeContainer">
            <div class="travelModeSelector">
              <div
                data-travelmode="DRIVING"
                class="travelMode travelMode__selected"
              >
                <button role="radio">
                  <img
                    alt="DRIVING"
                    class="iconTravelMode iconTravelMode__DRIVING"
                    src="https://images.woosmap.com/directions/drive_black.png"
                  />
                  <img
                    alt="DRIVING"
                    class="iconTravelMode iconTravelMode__DRIVING__selected"
                    src="https://images.woosmap.com/directions/drive_selected.png"
                  />
                </button>
              </div>
              <div data-travelmode="WALKING" class="travelMode">
                <button role="radio">
                  <img
                    alt="WALKING"
                    class="iconTravelMode iconTravelMode__WALKING"
                    src="https://images.woosmap.com/directions/walk_black.png"
                  />
                  <img
                    alt="WALKING"
                    class="iconTravelMode iconTravelMode__WALKING__selected"
                    src="https://images.woosmap.com/directions/walk_selected.png"
                  />
                </button>
              </div>
              <div data-travelmode="CYCLING" class="travelMode">
                <button role="radio">
                  <img
                    alt="CYCLING"
                    class="iconTravelMode...

CSS

@charset "UTF-8";
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 {
  flex-basis: 18rem;
  box-shadow: 0 -2px 4px 0 rgba(0, 0, 0, 0.12);
  z-index: 1;
}

#mapContainer {
  display: flex;
  flex-direction: column;
  flex-basis: 70vw;
  flex-grow: 5;
  position: relative;
}

.tableContainer {
  max-height: 35%;
  min-height: 150px;
  overflow-y: auto;
  font-size: 13px;
}
.tableContainer table {
  border-collapse: collapse;
  width: 100%;
}
.tableContainer tr:nth-child(even) {
  background-color: #f5f5f5;
}
.tableContainer tr:nth-child(odd) {
  background-color: #ffffff;
}
.tableContainer thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}
.tableContainer th {
  background: #eee;
  font-weight: bold;
}
.tableContainer td {
  white-space: nowrap;
}
.tableContainer th, .tableContainer td {
  text-align: center;
  padding: 8px 16px;
}
.tableContainer tr td:first-child span, .tableContainer tr td:nth-child(2) span {
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  color: white;
  text-align: center;
  display: inline-block;
  width: 20px;
  height: 20px;
  font-size: 80%;
  font-weight: 600;
  line-height: 14px;
}
.tableContainer tr td:first-child span {
  background-image: url("https://images.woosmap.com/marker-blue.svg");
}
.tableContainer tr td:first-child {
  position: relative;
}
.tableContainer tr td:first-child:after {
  position: absolute;
  right: 0;
  color: #222;
  content: "→";
  top: 12px;
}
.tableContainer tr td:nth-child(2) span {
 ...

JavaScript

let map;
let bounds;
let distanceService;
let distanceRequest;
let markersArray = [];
let originsList;
let destinationsList;
const origins = [];
const destinations = [];
let line = null;

function createMarker(position, label, url) {
  return new woosmap.map.Marker({
    map,
    position,
    icon: {
      url,
      labelOrigin: new woosmap.map.Point(13, 15),
      scaledSize: {
        height: 38,
        width: 26,
      },
    },
    label: {
      text: label,
      color: "white",
    },
  });
}

function clearMarkers() {
  for (const marker of markersArray) {
    marker.setMap(null);
  }

  markersArray = [];
}

function displayMatrixMarkers(origins, destinations) {
  clearMarkers();
  origins.forEach((origin, index) => {
    bounds.extend(origin);
    markersArray.push(
      createMarker(
        origin,
        (index + 1).toString(),
        "https://images.woosmap.com/marker-blue.svg",
      ),
    );
  });
  destinations.forEach((destination, index) => {
    bounds.extend(destination);
    markersArray.push(
      createMarker(
        destination,
        (index + 1).toString(),
        "https://images.woosmap.com/marker-red.svg",
      ),
    );
  });
  map.fitBounds(bounds, { top: 70, bottom: 40, left: 50, right: 50 }, true);
}

function createDefaultRequest() {
  origins.push({ lat: 51.6511, lng: -0.1615 }, { lat: 51.4269, lng: -0.0955 });
  destinations.push(
    { lat: 51.4855, lng: -0.3179 },
    { lat: 51.5146, lng: -0.0212 },
  );

  for (const origin of origins) {
    addLatLngToList(originsList, origin);
  }

  for (const destination of destinations) {
    addLatLngToList(destinationsList, destination);
  }
  return {
    origins,
    destinations,
    travelMode: woosmap.map.TravelMode.DRIVING,
    unitSystem: woosmap.map.UnitSystem.METRIC,
    avoidHighways: false,
    avoidTolls: false,
    elements: "duration_distance",
  };
}

function addLatLngToList(element, location) {
  const locationElement = document.createElement("li");

 ...