Skip to content

Instantly share code, notes, and snippets.

function toGeojson(timeMapResponse) {
// Parses the response json. This is needed if the parameter is a raw string. You don't need this if the response has already been parsed.
var responseData = JSON.parse(timeMapResponse);
// Converts each result in the response into a MultiPolygon object.
var multiPolygons = responseData['results'].map(r => shapesToMultiPolygon(r['shapes']));
// Returns a feature collection object. This can be written to a file or sent for further processing.
return {
'type': 'FeatureCollection',
'features': multiPolygons
};
function shapesToMultiPolygon(shapes) {
var allRings = shapes.map(function (shape) {
// The shell of the polygon is in a separate property 'shell'.
var shell = remapLinearRing(shape['shell']);
// The 'holes' property is an array of linear rings for holes.
var holes = shape['holes'].map(h => remapLinearRing(h));
// Combine the shell and holes so that the shell is the first element in the resulting array.
return [shell] + holes;
});
// returns an object adhering to GeoJson Multipolygon specification.
function remapLinearRing(linearRing) {
// Converts an array of { 'lat': <lat>, 'lng': <lng> } objects into an array of [<lng>, <lat>].
return linearRing.map(c => [c['lng'], c['lat']])
};
{
"results": [
{
"search_id": "7f45bc64-b567-4453-9477-50d4ce928690",
"shapes": [
{
"shell": [
{
"lat": 51.51180324,
"lng": -0.17239974000000036
@LouisaKB
LouisaKB / Response body using TimeMap
Created September 10, 2018 12:59
Example TravelTime API response body using TimeMap
{
"results": [
{
"search_id": "public transport from Trafalgar Square",
"shapes": [
{
"shell": [
{
"lat": 51.516246,
"lng": -0.14439687999999948
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v1.3.0/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v1.3.0/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
<div id="map" style="height: 100%;"></div>
<script>
sendGeocodingRequest(locationCoordinates);
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v1.3.0/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v1.3.0/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.devbridge-autocomplete/1.4.8/jquery.autocomplete.min.js"></script>
<style>
.autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; }
.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
.autocomplete-selected { background: #F0F0F0; }
// Creates a function that responds to the selected suggestion.
function moveMarker(map, marker) {
return function (suggestion) {
// The Geocoding API returns coordinates in a [<lng>, <lat>] format. We transform it into Leaflet's LatLng.
var latLng = L.latLng(suggestion.data.reverse());
// Change the coordintates for the marker.
marker.setLatLng(latLng);
// Pan to the map view the new coordinates.
map.flyTo(latLng);
// Send a Time Map request and draw the isochrone.
// 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",