Mapping Santa’s Route with HERE APIs
As the holidays are approaching, the world is getting ready for the magical night with Santa Claus bringing joy to millions of children. This year, we at HERE Technologies, had the unique opportunity to assist Santa Claus with his logistics. Let’s look at how HERE Geocoding and Search API, HERE Routing API and HERE Tour Planning are used to ensure that Santa can deliver gifts to millions of children across the globe in just one night.
Geocoding the chimneys
First, we got the naughty and nice list from Santa. Using the HERE Geocoding and Search API, we converted the textual addresses into precise geographical coordinates and integrated these into Santa’s dashboard. With global data coverage from HERE APIs, this gave Santa access to 842 million children’s latitude and longitudes across the globe. Here’s the sample code of how Santa used this with the HERE Maps API for JavaScript:
// Initialize HERE Map
const platform = new H.service.Platform({
apikey: 'SANTA_API_KEY'
});
const defaultLayers = platform.createDefaultLayers();
const map = new H.Map(
document.getElementById('map'),
defaultLayers.vector.normal.map,
{
center: { lat: 90, lng: 0 }, // Start at the North Pole
zoom: 2
}
);
const ui = H.ui.UI.createDefault(map, defaultLayers);
// Get an instance of the HERE geocoding service:
const service = platform.getSearchService();
// Array of children's addresses to geocode
var addresses = [
'1234 Gingerbread Avenue, Gingerbread Town',
'7 Dwarfs Diamond Mine, Fairy Tale Forest',
'Elsa Room, Arendelle Castle, Arendelle',
'123 Candy Cane Lane, North Pole',
// more addresses
];
// Array to store the lat long of geocoded addresses
var waypoints = [];
// Function to handle geocoding for multiple locations
function geocodeAddresses(addresses) {
return Promise.all(addresses.map((address) => {
return new Promise((resolve) => {
service.geocode({
q: address
}, (result) => {
if (result.items.length > 0) {
const position = { //first result only
lat: result.items[0].position.lat,
lng: result.items[0].position.lng
};
waypoints.push(position);
var marker = new H.map.Marker(position);
map.addObject(marker);
}
resolve();
}, alert);
});
}));
}
Routing with waypoints
To circle the entire planet, Santa would have to travel 218 million miles!
Once we pass the locations to the geocoder, the HERE Routing API was implemented. Each chimney was added a waypoint using 'via' in the routing parameters. This created the most efficient delivery route for Santa.
// Call the function to geocode the addresses, and use it for routing
geocodeAddresses(addresses)
.then(() => {
const origin = { lat: 90, lng: 0 }; // Santa's workshop
const destination = { lat: 89.5, lng: 0 }; // Santa's home
const routingParameters = {
'routingMode': 'fast',
'transportMode': 'car', //should be 'sleigh'
'origin': `${origin.lat},${origin.lng}`,
'destination': `${destination.lat},${destination.lng}`,
'return': 'polyline',
'via': new H.service.Url.MultiValueQueryParameter(
waypoints.map(wp => `${wp.lat},${wp.lng}`)
)
};
const onResult = function(result) {
if (result.routes.length) {
const lineStrings = [];
result.routes[0].sections.forEach((section) => { lineStrings.push(H.geo.LineString.fromFlexiblePolyline(section.polyline));
});
const multiLineString = new H.geo.MultiLineString(lineStrings);
// Polyline to display the route
const routeLine = new H.map.Polyline(multiLineString, {
style: {
strokeColor: 'green',
lineWidth: 3
}
});
// Marker for the start point and end point
const startMarker = new H.map.Marker(origin);
const endMarker = new H.map.Marker(destination);
// Marker for the chimneys
const waypointMarkers = [];
waypoints.forEach((waypoint) => {
var waypointMarker = new H.map.Marker({
lat: waypoint.lat,
lng: waypoint.lng
});
waypointMarkers.push(waypointMarker);
});
// Group to hold all the map objects
const group = new H.map.Group();
group.addObjects([routeLine, startMarker, endMarker, ...waypointMarkers]);
// Add the group to the map
map.addObject(group);
// Set the map viewport to make the entire route visible
map.getViewModel().setLookAtData({
bounds: group.getBoundingBox()
});
};
};
// Get an instance of the routing service version 8:
const router = platform.getRoutingService(null, 8);
// calculateRoute() method with the routing parameters,
// the callback, and an error callback function
router.calculateRoute(routingParameters, onResult,
function(error) {
alert(error.message);
});
}).catch(error => {
console.error('Error:', error);
});
Tour Planning with delivery time window
Considering changing time zones, Santa has a total of 34 hours to deliver all gifts before sunrise. By considering the strict time window for each delivery, traffic conditions, and the load that can be carried, we decided to use the HERE Tour Planning API to manage his schedule. We considered 2 minutes at each house, to give enough time for Santa to deliver the gift as well as enjoy the cookies and milk.
Learn how the HERE Tour Planning requests can be constructed with specific delivery times and locations.
Conclusion
Through the seamless integration of HERE Geocoding and Search API, HERE Routing API and Tour Planning API, Santa Claus will be able to deliver the holiday spirit at every home within his magical time.
Happy holidays and merry mapping!
Have your say
Sign up for our newsletter
Why sign up:
- Latest offers and discounts
- Tailored content delivered weekly
- Exclusive events
- One click to unsubscribe