Node.Js How to Get Shipping Route Using OSRM API

December 16, 2019

It's been quite a while I didn't post anything. Well sorry, because I have to do my endless project and university tasks. In this article, I will be sharing my current small project using OSRM API.

Well, I assume that you may have already known what OSRM API is (because I guess it what leads you here). as a short introduction, OSRM API is an opensource API that lets developers get the distance, the needed time between "from (home, for example) and to (destination, for example)". 

Node.Js How to Get Shipping Route Using OSRM API


For instance, in my home town, there are 3 post offices (po-A, po-B, po-C), I wanna know the fastest route and time required from my home - which post office has the shortest way or less time required. For developer, to overcome this problem, OSRM can be the answer, because it is totally free and opensource (of course).

According to WIkipedia , The Open Source Routing Machine or OSRM is a C++ implementation of a high-performance routing engine for shortest paths in road networks. Licensed under the permissive 2-clause BSD license, OSRM is a free network service. OSRM supports Linux, FreeBSD, Windows, and Mac OS X platform.

So, it is so much like Google Maps. Yes, it is actually so much like Google maps. However, it's free

Usually, OSRM API is used by shipping project, and traveling app. It's a very nice API by the way. Ok, enough for the introduction. Let's get into the code.

in order to get the route the API we can youse is as follow
http://router.project-osrm.org/table/v1/driving/latitude,longitude;latiude,longitude;latiude,longitude?sources=0


Let me explain this first.
1. The first latitude, longitude is the source/ initial location. Let's say it is the home
2. The second and the third latitude, longitude is the destination. Let's assume it is Post office A, Post Office B, Post Office C. We can add the destination more than one, just simply get the longitude and latitude for each destination and separate it by a semicolon (;).

Requirements
Request module.

Code and explanation:


const request = require("request")

//help array variable
let destination_result = []

//osrm function
const osrm = (addressOrigin, adressDestination, callback) => {
    const url = `http://router.project-osrm.org/table/v1/driving/${addressOrigin};${adressDestination}?sources=0`;
    request({ url, json: true }, (err, res) => {
        if (err) {
            return callback("cannot connect", undefined);
        } else if (res.body.message === "Too Many Requests") {
            callback("too many request, please try again..", undefined);
        } else {
            const destination = res.body.destinations
            destination.forEach((dst, index) => {
                if (index !== 0) {
                    destination_result.push({
                        destination: res.body.destinations[index].location.toString(),
                        duration: res.body.durations[0][index],
                        distance: res.body.destinations[index].distance
                    })
                }
            });

            //SORTING THE NEAREST
            destination_result.sort((a, b) => (a.duration > b.duration) ? 1 : ((b.duration > a.duration) ? -1 : 0));

            console.log('destination result => ', destination_result)

            const result = {
                source: res.body.sources[0].location,
                destination_result
            }
            callback(undefined, result);
            // destination_result = []
        }
    });
};

//for example
const source = '17.060976,51.115321'
const destination = '17.063264,51.114397;17.035318,51.107706;17.039807,51.108434'

//call the function
osrm(source, destination, (err, res) => {
    if (err) {
        return console.log(err)
    }
    console.log(res)
})

I use this API several times, and maybe I can tell you the pros and cons of this API
Pros
- open source
- free
-available documentation

Cons
-the documentation is a bit not "user-friendly"
- sometimes it takes time to load the result
- sometimes their server is "busy" and instead they will return "too many requests". It leads me to refresh my page and service several times

I hope it can help :)

For OSRM you can check here for the more information about the use of this API http://project-osrm.org/docs/v5.5.1/api

Tag:
Node.Js How to Get Shipping Route Using OSRM API, OSRM API, OSRM Node.js, OSRM API, osrm docker, osrm android, osrm install, osrm python, osrm r, osrm frontend, osrm windows, osrm traffic

Share this

Related Posts

Previous
Next Post »