Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:ifit/node-googlemaps

  • Loading branch information...
commit 873f60f11050d0897ad32c32f355a25fffa9f572 2 parents 910a3b7 + dbed612
@regality regality authored
Showing with 53 additions and 3 deletions.
  1. +53 −3 lib/googlemaps.js
View
56 lib/googlemaps.js
@@ -142,6 +142,9 @@ exports.elevationFromLocations = function(locations, callback, sensor) {
// http://code.google.com/apis/maps/documentation/elevation/#Paths
exports.elevationFromPath = function(path, samples, callback, sensor) {
+ if (config.encode-polylines){
+ path = 'enc:' + createEncodedPolyline(path);
+ }
var args = {
'path': path,
'samples': samples
@@ -250,9 +253,13 @@ exports.staticMap = function(center, zoom, size, callback, sensor ,
var points = paths[i].points;
if (points) {
- for (k = 0; k < points.length; k++) {
- new_path += '|' + points[k];
- }
+ if (config.encode-polylines){
+ new_path += '|enc:' + createEncodedPolyilne(points);
+ } else {
+ for (k = 0; k < points.length; k++) {
+ new_path += '|' + points[k];
+ }
+ }
}
args.path[i] = new_path.replace(/^\|/, '');
}
@@ -344,6 +351,49 @@ var returnObjectFromJSON = function(callback) {
return false;
};
+function createEncodedPolyline(points) {
+ var i, dlat, dlng;
+ var plat = 0;
+ var plng = 0;
+ var encoded_points = "";
+
+ for(i = 0; i < points.length; i++) {
+ var point = points[i];
+ var lat = point[0];
+ var lng = point[1];
+ var late5 = Math.round(lat * 1e5);
+ var lnge5 = Math.round(lng * 1e5);
+ dlat = late5 - plat;
+ dlng = lnge5 - plng;
+ plat = late5;
+ plng = lnge5;
+ encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
+ }
+ return encoded_points;
+}
+
+
+function encodeNumber(num) {
+ var encodeString = "";
+ var nextValue, finalValue;
+ while (num >= 0x20) {
+ nextValue = (0x20 | (num & 0x1f)) + 63;
+ encodeString += (String.fromCharCode(nextValue));
+ num >>= 5;
+ }
+ finalValue = num + 63;
+ encodeString += (String.fromCharCode(finalValue));
+ return encodeString;
+}
+
+function encodeSignedNumber(num) {
+ var sgn_num = num << 1;
+ if (num < 0) {
+ sgn_num = ~(sgn_num);
+ }
+ return(encodeNumber(sgn_num));
+}
+
function buildUrl(path, args) {
if (googleClientId && googlePrivateKey) {
args.client = googleClientId;
Please sign in to comment.
Something went wrong with that request. Please try again.