BeNdErR BeNdErR - 4 months ago 15
Javascript Question

Google Elevation call works with browser but not with node.js

TL;DR



I'm building an application with node.js that retrieves the elevation, starting from an array of latitude/longidtude points.

To get the elevation from a lat/lon coordinate, I use GoogleMaps APIs, and in particular Google Elevation APIs (find docs here)

Why does this url

// FOR OBVIOUS REASONS, THE API_KEY WAS REPLACED WITH XXXX_XXXX

https://maps.googleapis.com/maps/api/elevation/json?locations=enc:wyitGexebA?PB\&key=XXXX_XXXX


work and return data via browser while the same does not happen in node.js, resulting in the following response error?

{ results: [], status: 'INVALID_REQUEST' }





For the bravest



Here are the coordinates I'm using and that are giving me troubles:

lat="45.4391630", lon="11.0120330"
lat="45.4391600", lon="11.0119420"
lat="45.4391410", lon="11.0117930"


I'm using polyline module to convert the coordinates with the Polyline Algorithm and it works great. The coordinates are converted to the following string:

wyitGexebA?PB\


You can double check the validity of the result directly from Google's Polilyne utility here, but I guarantee that the string is correct.

So far no problems, I happily build the request url and here's the result

// FOR OBVIOUS REASONS, THE API_KEY WAS REPLACED WITH XXXX_XXXX

https://maps.googleapis.com/maps/api/elevation/json?locations=enc:wyitGexebA?PB\&key=XXXX_XXXX


The url works like a charm in the browser, the output is the following

{
"results" : [
{
"elevation" : 57.68282699584961,
"location" : {
"lat" : 45.43916,
"lng" : 11.01203
},
"resolution" : 19.08790397644043
},
{
"elevation" : 57.85408401489258,
"location" : {
"lat" : 45.43916,
"lng" : 11.01194
},
"resolution" : 19.08790397644043
},
{
"elevation" : 58.09893417358398,
"location" : {
"lat" : 45.43914,
"lng" : 11.01179
},
"resolution" : 19.08790397644043
}
],
"status" : "OK"
}


Unfortunately, the same url does not work within node. I'm using request module here to get the dirty work done as follows:

var polyline = require('polyline');
var request = require('request');

var poly = polyline.encode([
['45.4391630', '11.0120330'],
['45.4391600', '11.0119420'],
['45.4391410', '11.0117930']
]);
var url = 'https://maps.googleapis.com/maps/api/elevation/json?locations=enc:' + poly + '&key=XXXX_XXXX';
request(url, function(err, res, body){
if(err || res.statusCode != 200){
console.log({err: err, status: 'Erorr'});
}else{
console.log(url);
console.log(JSON.parse(body));
}
});


Running this with node, prints the following in the console:

https://maps.googleapis.com/maps/api/elevation/json?locations=enc:wyitGexebA?PB\&key=XXXX_XXXX
{ results: [], status: 'INVALID_REQUEST' }


The issue is not related with the code logic as the same logic was used to retrieve from 15k to 30k elevations with no problems, if you don't believe me (and you should) you can test the following coordinates

var poly = polyline.encode([
['45.4393160', '11.0129670'],
['45.4393160', '11.0129200'],
['45.4392910', '11.0128690']
]);


Any hint or help? Why this different behaviour? Thanks in advance

Answer

Depending on how you're forming your strings, that backslash might be tripping you up. The latter section of your URL:

enc:wyitGexebA?PB\&key=XXXX_XXXX

when plonked into a string, that backslash will be seen as an escape character and vanish. Replace it with %5C via encodeURIComponent (or just run the whole URI through encodeURI)

Comments