Skip to content

Instantly share code, notes, and snippets.

@LouisaKB
Created August 23, 2018 12:02
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 LouisaKB/d85aa19ef25c25220a793bd2924713dc to your computer and use it in GitHub Desktop.
Save LouisaKB/d85aa19ef25c25220a793bd2924713dc to your computer and use it in GitHub Desktop.
// Sends the request of the Time Map multipolygon.
function sendTimeMapRequest(coordinates, map) {
var latLng = { lat: coordinates[0], lng: coordinates[1] };
var departureTime = new Date().toISOString();
var travelTime = 30*60; // 30 minutes
var request = {
departure_searches: [ {
id: "first_location",
"coords": latLng,
transportation: {
type: "driving"
},
departure_time: departureTime,
travel_time: travelTime
} ],
arrival_searches: []
};
$.ajax({
url: "http://api.traveltimeapp.com/v4/time-map",
type: "post",
headers: authHeaders,
data: JSON.stringify(request),
contentType: "application/json; charset=UTF-8",
success: drawTimeMap(map)
});
};
// A helper function that converts [{lat: <lat>, lng: <lng>}, ...] to a [[<lat>, <lng>], ...] format.
function ringCoordsHashToArray(ring) {
return ring.map((latLng) => { return [latLng.lat, latLng.lng]; } );
};
// Draws the resulting multipolygon from the response on the map.
function drawTimeMap(map) {
// We are returning a function so that it can be easily used in the success parameter of the ajax method.
return (response) => {
// Reference for the response: http://docs.traveltimeplatform.com/reference/time-map/#response-body-json-attributes
var shapesCoords = response.results[0].shapes.map((polygon) => {
var shell = ringCoordsHashToArray(polygon.shell);
var holes = polygon.holes.map(ringCoordsHashToArray);
return [shell].concat(holes);
})
var polygon = L.polygon(shapesCoords, {color: 'red'});
polygon.addTo(map);
map.fitBounds(polygon.getBounds());
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment