polyline-encoded
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "polyline-encoded", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Encoding and decoding of Google's polyline encoding format", | ||
@@ -5,0 +5,0 @@ "main": "Polyline.encoded.js", |
@@ -14,11 +14,11 @@ /* | ||
/*jshint browser:true, debug: true, strict:false, globalstrict:false, indent:4, white:true, smarttabs:true*/ | ||
/*global L:true, console:true*/ | ||
(function () { | ||
'use strict'; | ||
/* jshint bitwise:false */ | ||
(function () { | ||
// This function is very similar to Google's, but I added | ||
// some stuff to deal with the double slash issue. | ||
var encodeNumber = function (num) { | ||
var encodeString = ""; | ||
var encodeString = ''; | ||
var nextValue, finalValue; | ||
@@ -60,19 +60,20 @@ while (num >= 0x20) { | ||
var PolylineUtil = { | ||
encode: function (latlngs) { | ||
encode: function (latlngs, precision) { | ||
var i, dlat, dlng; | ||
var plat = 0; | ||
var plng = 0; | ||
var encoded_points = ""; | ||
var encoded_points = ''; | ||
precision = Math.pow(10, precision || 5); | ||
for (i = 0; i < latlngs.length; i++) { | ||
var lat = getLat(latlngs[i]); | ||
var lng = getLng(latlngs[i]); | ||
var late5 = Math.floor(lat * 1e5); | ||
var lnge5 = Math.floor(lng * 1e5); | ||
dlat = late5 - plat; | ||
dlng = lnge5 - plng; | ||
plat = late5; | ||
plng = lnge5; | ||
var latFloored = Math.floor(lat * precision); | ||
var lngFloored = Math.floor(lng * precision); | ||
dlat = latFloored - plat; | ||
dlng = lngFloored - plng; | ||
plat = latFloored; | ||
plng = lngFloored; | ||
encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng); | ||
@@ -83,3 +84,3 @@ } | ||
decode: function (encoded) { | ||
decode: function (encoded, precision) { | ||
var len = encoded.length; | ||
@@ -91,2 +92,4 @@ var index = 0; | ||
precision = Math.pow(10, -(precision || 5)); | ||
while (index < len) { | ||
@@ -114,3 +117,3 @@ var b; | ||
latlngs.push([lat * 1e-5, lng * 1e-5]); | ||
latlngs.push([lat * precision, lng * precision]); | ||
} | ||
@@ -121,2 +124,3 @@ | ||
}; | ||
/* jshint bitwise:true */ | ||
@@ -123,0 +127,0 @@ // Export Node module |
@@ -6,3 +6,3 @@ # Support encoded polylines in Leaflet | ||
# Provided methods # | ||
### Provided methods | ||
@@ -14,8 +14,9 @@ <table> | ||
<tr> | ||
<td><code>L.PolylineUtil.encode(latlngs)</code></td> | ||
<td>Encode an array of <code>L.LatLng</code> objects.</td> | ||
<td><code>L.PolylineUtil.encode(latlngs [, precision])</code></td> | ||
<td>Encode an array of <code>L.LatLng</code> objects, | ||
or an array of arrays.</td> | ||
</tr> | ||
<tr> | ||
<td><code>L.PolylineUtil.decode(encoded)</code></td> | ||
<td>Decode the string <code>encoded</code> to an array of <code>L.LatLng</code> objects.</td> | ||
<td><code>L.PolylineUtil.decode(encoded [, precision])</code></td> | ||
<td>Decode the string <code>encoded</code> to an array of <code>[lat, lng]</code>-arrays.</td> | ||
</tr> | ||
@@ -47,14 +48,14 @@ | ||
# Code examples | ||
### Code examples | ||
After loading ```leaflet.js```, ```src/Polyline.encoded.js``` should be included. | ||
## Encoding | ||
### Encoding | ||
```javascript | ||
var latlngs = [ | ||
new L.LatLng(38.5, -120.5), | ||
new L.LatLng(40.7, -120.95), | ||
new L.LatLng(43.252, -126.453) | ||
[38.5, -120.5], | ||
[40.7, -120.95], | ||
[43.252, -126.453] | ||
]; | ||
var polyline = new L.Polyline(latlngs); | ||
var polyline = L.polyline(latlngs); | ||
@@ -65,3 +66,3 @@ //prints "_p~iF~cn~U_ulLn{vA_mqNvxq`@" to the console | ||
## Decoding | ||
#### Decoding | ||
```javascript | ||
@@ -75,3 +76,13 @@ var encoded = "_p~iF~cn~U_ulLn{vA_mqNvxq`@"; | ||
# Node package | ||
Use a decoding precision of 6 to decode OSRM Routing Engine geometries | ||
```javascript | ||
var encoded = "_izlhA~pvydF_{geC~{mZ_kwzCn`{nI"; | ||
var polyline = new L.Polyline(PolylineUtil.decode(encoded, 6)); | ||
// prints an array of 3 LatLng objects. | ||
console.log(polyline.getLatLngs()); | ||
``` | ||
### Node package | ||
You can use `encode()` and `decode()` in your Nodejs scripts: | ||
@@ -81,2 +92,7 @@ | ||
And then just `require('polyline-encoded')` | ||
```javascript | ||
var polylineEncoded = require('polyline-encoded'); | ||
var encoded = "_p~iF~cn~U_ulLn{vA_mqNvxq`@"; | ||
var latlngs = polylineEncoded.decode(encoded); | ||
``` |
@@ -0,1 +1,4 @@ | ||
'use strict'; | ||
/* global describe:true, it:true */ | ||
var polyUtil = require('../Polyline.encoded.js'); | ||
@@ -11,3 +14,5 @@ var expect = require('expect.js'); | ||
]; | ||
var encoded = '_p~iF~cn~U_ulLn{vA_mqNvxq`@'; | ||
var encoded6 = '_izlhA~pvydF_{geC~{mZ_kwzCn`{nI'; | ||
@@ -20,2 +25,6 @@ describe('Polyline', function () { | ||
it('encodes with precision = 6', function () { | ||
expect(polyUtil.encode(latlngs, 6)).to.eql(encoded6); | ||
}); | ||
it('decodes', function () { | ||
@@ -28,2 +37,10 @@ var decoded = polyUtil.decode(encoded); | ||
}); | ||
it('decodes with precision = 6', function () { | ||
var decoded = polyUtil.decode(encoded6, 6); | ||
for (var i in decoded) { | ||
expect(decoded[i][0]).to.be.within(latlngs[i][0] - d, latlngs[i][0] + d); | ||
} | ||
}); | ||
}); |
13429
10
178
93