Work with Distance

Detailed guide on using the DistanceService to calculate distances and durations between multiple origins and destinations or to compute isochrones.

  1. Overview
  2. Getting started
  3. Distance Matrix
  4. Distance Isochrone

Overview

The DistanceService class is a part of the Woosmap Map JavaScript API that allows you to calculate distances and durations between multiple origins and destinations. It provides methods for distance matrix and distance isochrone calculations.

Following operations are supported:

Review the example below to see how to use the DistanceService and display results using the Woosmap Map JS:

Getting started

Before using the DistanceService in the Map JavaScript API, it is required to have a secure and valid Woosmap API key and enable the Distance API for it.

Next, you must load the Map JS API library by including a script tag in your HTML page as follows:

HTML
        <script async
        src="https://sdk.woosmap.com/map/map.js?key=YOUR_API_KEY&callback=initMap">
</script>

    

Distance Matrix

Request Distance Matrix

To request a distance matrix calculation, instantiate a DistanceService object and invoke DistanceService.getDistanceMatrix(). This method sends a request to the Distance Matrix endpoint of Woosmap Distance API, using a DistanceMatrixRequest object literal.

The DistanceMatrixRequest object literal contains the following fields:

Type definition for DistanceMatrixRequest
        {
  origins: woosmap.map.LatLng[] | woosmap.map.LatLngLiteral[];
  destinations: woosmap.map.LatLng[] | woosmap.map.LatLngLiteral[];
  method?: "time" | "distance";
  elements?: "distance" | "duration" | "duration_distance";
  departureTime?: Date | string;  
  language?: string;
  travelMode?: woosmap.map.TravelMode;
  unitSystem?: woosmap.map.unitSystem;
  avoidFerries?: boolean;
  avoidHighways?: boolean;
  avoidTolls?: boolean;
  avoidZones?: woosmap.map.LatLng[][] | woosmap.map.LatLngLiteral[][];
}

    

Below is a sample DistanceMatrixRequest for two origins to two destinations using driving mode:

JavaScript
        const distanceMatrixRequest = {
    origins: [{lat: 45.4642, lng: 9.19}, {lat: 45.75, lng: 4.85}],
    destinations: [{lat: 42.6976, lng: 9.45}, {lat: 41.9028, lng: 12.4964}],
    travelMode: woosmap.map.TravelMode.DRIVING,
    unitSystem: woosmap.map.unitSystem.METRIC,
    avoidHighways: true,
    avoidTolls: true
};

    

The DistanceService’s getDistanceMatrix() method is asynchronous and returns a Promise object that is either fulfilled with a DistanceMatrixResponse object upon successful operation or rejected with an APIError object. You can attach handlers to this Promise object to process results upon operation success or failure.

JavaScript
        const distanceService = new woosmap.map.DistanceService();
distanceService.getDistanceMatrix(distanceMatrixRequest)
    .then((response) => {
        console.log(response);
    })
    .catch((error) => {
        console.error(error);
    });

    

Distance Matrix Response

The response to a DistanceService.getDistanceMatrix request consists of a status, and a list of DistanceMatrixResponseRows, one for each corresponding origin location.

Distance Isochrone

An isochrone is a line that connects points of equal travel time around a given location. Here is a sample of a Distance Isochrone API call using Javascript and displaying the isoline result on Woosmap Map.

Request Distance Isochrone

To request a distance isochrone calculation, instantiate a DistanceService object and invoke DistanceService.getDistanceIsochrone(). This method sends a request to the Distance Isochrones endpoint of Woosmap Distance API, using a DistanceIsochroneRequest object literal.

The DistanceIsochroneRequest object literal contains the following fields:

Type definition for DistanceIsochroneRequest
        {
  origin: woosmap.map.LatLng | woosmap.map.LatLngLiteral;
  value?: number;
  language?: string;
  method?: "time" | "distance";
  travelMode?: string;
  unitSystem?: string;
  avoidFerries?: boolean;
  avoidHighways?: boolean;
  avoidTolls?: boolean;
  avoidZones?: woosmap.map.LatLng[][] | woosmap.map.LatLngLiteral[][];
};

    

See Request Distance Matrix for the description of the common fields.

Below is a sample DistanceIsochroneRequest to compute a 10 minutes isochrone from a given location using driving mode:

JavaScript
        const distanceIsochroneRequest = {
    origin: {lat: 45.4642, lng: 9.19},
    travelMode: woosmap.map.TravelMode.DRIVING,
    value: 10,    
};

    

The DistanceService’s getDistanceIsochrone() method is asynchronous and returns a Promise object that is either fulfilled with a DistanceIsochroneResponse object upon successful operation or rejected with an APIError object. You can attach handlers to this Promise object to process results upon operation success or failure.

JavaScript
        const distanceService = new woosmap.map.DistanceService();
distanceService.getDistanceIsochrone(distanceIsochroneRequest)
    .then((response) => {
        console.log(response);
    })
    .catch((error) => {
        console.error(error);
    });

    

Display Distance Isochrone

The DistanceIsochroneResponse includes a DistanceIsoline with a path attribute. This attribute represents the decoded polyline of the isoline, which can be used to visualize the isochrone on the map. Here is an example of how to display this polyline using the woosmap.map.Polyline layer of the Woosmap Map JS API:

JavaScript
        const myMap = new woosmap.map.Map(document.getElementById("map"));

const distanceService = new woosmap.map.DistanceService();
distanceService
        .getDistanceIsochrone(distanceIsochroneRequest)
        .then(displayIsochrone)
        .catch((error) => {
          console.error(error);
        });

function displayIsochrone(isochrone) {
  const polyline = new woosmap.map.Polyline({
    path: isochrone.isoline.path,
    strokeColor: "#b71c1c",
    strokeOpacity: 0.8,
    strokeWeight: 4
  });
  polyline.setMap(myMap);
}

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