Skip to content

Instantly share code, notes, and snippets.

@TravelTime-Frontend
Created February 21, 2023 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravelTime-Frontend/df2ec3595f43dcbef06e6018c6022259 to your computer and use it in GitHub Desktop.
Save TravelTime-Frontend/df2ec3595f43dcbef06e6018c6022259 to your computer and use it in GitHub Desktop.
// Draws the resulting multipolygon from the response on the map.
// We take the first result from our response since we requested only one shape.
// Reference for the response: http://docs.traveltimeplatform.com/reference/time-map/#response-body-json-attributes
var shapesCoords = response.results[0].shapes.map((polygon) => {
// The polygon is made of a shell and holes enclosed in a hash.
// The polygon ring is in a format of [{lat: <lat>, lng:<lng>}, ...] so we use the helper function to convert it to a format used by Leaflet's polygon factory.
var shell = ringCoordsHashToArray(polygon.shell);
var holes = polygon.holes.map(ringCoordsHashToArray);
// Leaflet's polygon factory accepts an array of of rings and then interprets the first ring as the shell and the rest of the rings as holes.
return [shell].concat(holes);
});
// Creates a multypolygon from an array of polygons with holes.
var polygon = L.polygon(shapesCoords, { color: 'red' });
// Adds the multipolygon to the map.
polygon.addTo(map);
// Fits the multipolygon to the map viewable area.
map.fitBounds(polygon.getBounds());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment