-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOSRM.RoutingGeometry.js
75 lines (61 loc) · 2.47 KB
/
OSRM.RoutingGeometry.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
// OSRM routing geometry
// [renders routing geometry]
var OSRM = {};
OSRM.CONSTANTS = {};
OSRM.CONSTANTS.PRECISION = 6;
OSRM.RoutingGeometry = {
// show route geometry - if there is a route
show: function(response) {
var geometry = OSRM.RoutingGeometry._decode(response.route_geometry, OSRM.C.PRECISION );
OSRM.G.route.showRoute(geometry, OSRM.Route.ROUTE);
},
//show route geometry - if there is no route
showNA: function() {
var positions = [];
for(var i=0, size=OSRM.G.markers.route.length; i<size; i++)
positions.push( OSRM.G.markers.route[i].getPosition() );
OSRM.G.route.showRoute(positions, OSRM.Route.NOROUTE);
},
//decode compressed route geometry
_decode: function(encoded, precision) {
precision = Math.pow(10, -precision);
var len = encoded.length, index=0, lat=0, lng = 0, array = [];
while (index < len) {
var b, shift = 0, result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
//array.push( {lat: lat * precision, lng: lng * precision} );
array.push( [lat * precision, lng * precision] );
}
return array;
}
};
module.exports = OSRM;