@digitransit/

Polyline_Decode

Nodejs

Decodes a Google encoded polyline and prints the list of points

fork
loading
Files
  • index.js
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const request = require('request');
const polyline = require('polyline');

var req = {
  url: 'https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql',
  method: 'POST',
  headers: { "Content-Type": "application/graphql" },
  body: `{ 
          plan(
            from: {lat: 60.18790, lon: 24.96123}
            to: {lat: 60.17091, lon: 24.94190}
            numItineraries: 1
          ) {
            itineraries {
              legs {
                from {
                  name
                }
                to {
                  name
                }
                legGeometry {
                  length
                  points
                }
              }
            }
          }
        }`
};

request(req, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var plan = JSON.parse(body).data.plan;

    var itinerary = plan.itineraries[0];

    itinerary.legs.forEach(function(leg) {
      console.log("Points for leg "+leg.from.name+" -> "+leg.to.name);
      var legGeometry = leg.legGeometry;

      //Decode polyline using polyline library
      var points = polyline.decode(legGeometry.points);
      points.forEach(function(point) {
        console.log(point[0]+","+point[1]);
      });
    });
  }
});
node v10.16.0