Distance Isochrone

Distance Isochrones lets you calculate distance isochrones from a given location.

  1. Example
  2. Running the Sample Locally

Example

Distance Isochrone
        let map: woosmap.map.Map;
let methodSelect, modeSelect: HTMLSelectElement;
let distanceInput: HTMLInputElement;
let marker: woosmap.map.Marker;
let polyline: woosmap.map.Polyline;
let distanceService: woosmap.map.DistanceService;

const markerIcon: woosmap.map.Icon = {
  url: "https://images.woosmap.com/icons/pin-red.png",
  scaledSize: {
    height: 38,
    width: 26,
  },
};
const centerLatLng: woosmap.map.LatLngLiteral = {
  lat: 51.5,
  lng: -0.234,
};
function createMarker(
  position: woosmap.map.LatLngLiteral,
  icon: woosmap.map.Icon,
  map: woosmap.map.Map,
) {
  const marker = new window.woosmap.map.Marker({
    position,
    icon,
  });
  marker.setMap(map);
  return marker;
}

function initMap() {
  map = new window.woosmap.map.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 9,
      center: centerLatLng,
    },
  );
  distanceService = new woosmap.map.DistanceService();
  marker = createMarker(centerLatLng, markerIcon, map);
  map.addListener("click", (e) => {
    marker.setMap(null);
    marker = createMarker(e.latlng, markerIcon, map);
    computeIsochrone(e.latlng);
  });
  const onChangeHandler = () => {
    computeIsochrone(marker.getPosition().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);
  }
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

    
        let map;
let methodSelect, modeSelect;
let distanceInput;
let marker;
let polyline;
let distanceService;
const markerIcon = {
  url: "https://images.woosmap.com/icons/pin-red.png",
  scaledSize: {
    height: 38,
    width: 26,
  },
};
const centerLatLng = {
  lat: 51.5,
  lng: -0.234,
};

function createMarker(position, icon, map) {
  const marker = new window.woosmap.map.Marker({
    position,
    icon,
  });

  marker.setMap(map);
  return marker;
}

function initMap() {
  map = new window.woosmap.map.Map(document.getElementById("map"), {
    zoom: 9,
    center: centerLatLng,
  });
  distanceService = new woosmap.map.DistanceService();
  marker = createMarker(centerLatLng, markerIcon, map);
  map.addListener("click", (e) => {
    marker.setMap(null);
    marker = createMarker(e.latlng, markerIcon, map);
    computeIsochrone(e.latlng);
  });

  const onChangeHandler = () => {
    computeIsochrone(marker.getPosition().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);
  }
}

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%;
}

#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>
      <div id="map"></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:

  1. Clone the repository and navigate to the directory of the sample.

  2. Install the necessary dependencies.

  3. Start running the sample.

Here are the commands you can use in your terminal to do this:

Shell
        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.

Shell
        git checkout sample/SAMPLE_NAME
npm i
npm start

    
Was this article helpful?
Have more questions? Submit a request