2 * Utility functions to decode/encode numbers and array's of numbers
3 * to/from strings (Google maps polyline encoding)
5 * Extends the L.Polyline and L.Polygon object with methods to convert
6 * to and create from these strings.
8 * Jan Pieter Waagmeester <jieter@jieter.nl>
11 * http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
12 * (which is down as of december 2014)
18 var defaultOptions = function (options) {
19 if (typeof options === 'number') {
25 options = options || {};
28 options.precision = options.precision || 5;
29 options.factor = options.factor || Math.pow(10, options.precision);
30 options.dimension = options.dimension || 2;
35 encode: function (points, options) {
36 options = defaultOptions(options);
39 for (var i = 0, len = points.length; i < len; ++i) {
40 var point = points[i];
42 if (options.dimension === 2) {
43 flatPoints.push(point.lat || point[0]);
44 flatPoints.push(point.lng || point[1]);
46 for (var dim = 0; dim < options.dimension; ++dim) {
47 flatPoints.push(point[dim]);
52 return this.encodeDeltas(flatPoints, options);
55 decode: function (encoded, options) {
56 options = defaultOptions(options);
58 var flatPoints = this.decodeDeltas(encoded, options);
61 for (var i = 0, len = flatPoints.length; i + (options.dimension - 1) < len;) {
64 for (var dim = 0; dim < options.dimension; ++dim) {
65 point.push(flatPoints[i++]);
74 encodeDeltas: function (numbers, options) {
75 options = defaultOptions(options);
79 for (var i = 0, len = numbers.length; i < len;) {
80 for (var d = 0; d < options.dimension; ++d, ++i) {
82 var delta = num - (lastNumbers[d] || 0);
89 return this.encodeFloats(numbers, options);
92 decodeDeltas: function (encoded, options) {
93 options = defaultOptions(options);
97 var numbers = this.decodeFloats(encoded, options);
98 for (var i = 0, len = numbers.length; i < len;) {
99 for (var d = 0; d < options.dimension; ++d, ++i) {
100 numbers[i] = Math.round((lastNumbers[d] = numbers[i] + (lastNumbers[d] || 0)) * options.factor) / options.factor;
107 encodeFloats: function (numbers, options) {
108 options = defaultOptions(options);
110 for (var i = 0, len = numbers.length; i < len; ++i) {
111 numbers[i] = Math.round(numbers[i] * options.factor);
114 return this.encodeSignedIntegers(numbers);
117 decodeFloats: function (encoded, options) {
118 options = defaultOptions(options);
120 var numbers = this.decodeSignedIntegers(encoded);
121 for (var i = 0, len = numbers.length; i < len; ++i) {
122 numbers[i] /= options.factor;
128 encodeSignedIntegers: function (numbers) {
129 for (var i = 0, len = numbers.length; i < len; ++i) {
130 var num = numbers[i];
131 numbers[i] = (num < 0) ? ~(num << 1) : (num << 1);
134 return this.encodeUnsignedIntegers(numbers);
137 decodeSignedIntegers: function (encoded) {
138 var numbers = this.decodeUnsignedIntegers(encoded);
140 for (var i = 0, len = numbers.length; i < len; ++i) {
141 var num = numbers[i];
142 numbers[i] = (num & 1) ? ~(num >> 1) : (num >> 1);
148 encodeUnsignedIntegers: function (numbers) {
150 for (var i = 0, len = numbers.length; i < len; ++i) {
151 encoded += this.encodeUnsignedInteger(numbers[i]);
156 decodeUnsignedIntegers: function (encoded) {
162 for (var i = 0, len = encoded.length; i < len; ++i) {
163 var b = encoded.charCodeAt(i) - 63;
165 current |= (b & 0x1f) << shift;
168 numbers.push(current);
179 encodeSignedInteger: function (num) {
180 num = (num < 0) ? ~(num << 1) : (num << 1);
181 return this.encodeUnsignedInteger(num);
184 // This function is very similar to Google's, but I added
185 // some stuff to deal with the double slash issue.
186 encodeUnsignedInteger: function (num) {
187 var value, encoded = '';
188 while (num >= 0x20) {
189 value = (0x20 | (num & 0x1f)) + 63;
190 encoded += (String.fromCharCode(value));
194 encoded += (String.fromCharCode(value));
200 // Export Node module
201 if (typeof module === 'object' && typeof module.exports === 'object') {
202 module.exports = PolylineUtil;
205 // Inject functionality into Leaflet
206 if (typeof L === 'object') {
207 if (!(L.Polyline.prototype.fromEncoded)) {
208 L.Polyline.fromEncoded = function (encoded, options) {
209 return L.polyline(PolylineUtil.decode(encoded), options);
212 if (!(L.Polygon.prototype.fromEncoded)) {
213 L.Polygon.fromEncoded = function (encoded, options) {
214 return L.polygon(PolylineUtil.decode(encoded), options);
219 encodePath: function () {
220 return PolylineUtil.encode(this.getLatLngs());
224 if (!L.Polyline.prototype.encodePath) {
225 L.Polyline.include(encodeMixin);
227 if (!L.Polygon.prototype.encodePath) {
228 L.Polygon.include(encodeMixin);
231 L.PolylineUtil = PolylineUtil;