]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.polyline.js
Merge remote-tracking branch 'upstream/master' into routing-merge
[rails.git] / vendor / assets / leaflet / leaflet.polyline.js
1 /*
2  * L.PolylineUtil contains utilify functions for polylines, two methods
3  * are added to the L.Polyline object to support creation of polylines
4  * from an encoded string and converting existing polylines to an
5  * encoded string.
6  *
7  *  - L.Polyline.fromEncoded(encoded [, options]) returns a L.Polyline
8  *  - L.Polyline.encodePath() returns a string
9  *
10  * Actual code from:
11  * http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/\
12  */
13
14 /*jshint browser:true, debug: true, strict:false, globalstrict:false, indent:4, white:true, smarttabs:true*/
15 /*global L:true, console:true*/
16
17
18 // Inject functionality into Leaflet
19 (function (L) {
20         if (!(L.Polyline.prototype.fromEncoded)) {
21                 L.Polyline.fromEncoded = function (encoded, options) {
22                         return new L.Polyline(L.PolylineUtil.decode(encoded), options);
23                 };
24         }
25         if (!(L.Polygon.prototype.fromEncoded)) {
26                 L.Polygon.fromEncoded = function (encoded, options) {
27                         return new L.Polygon(L.PolylineUtil.decode(encoded), options);
28                 };
29         }
30
31         var encodeMixin = {
32                 encodePath: function () {
33                         return L.PolylineUtil.encode(this.getLatLngs());
34                 }
35         };
36
37         if (!L.Polyline.prototype.encodePath) {
38                 L.Polyline.include(encodeMixin);
39         }
40         if (!L.Polygon.prototype.encodePath) {
41                 L.Polygon.include(encodeMixin);
42         }
43 })(L);
44
45 // Utility functions.
46 L.PolylineUtil = {};
47
48 L.PolylineUtil.encode = function (latlngs) {
49         var i, dlat, dlng;
50         var plat = 0;
51         var plng = 0;
52         var encoded_points = "";
53
54         for (i = 0; i < latlngs.length; i++) {
55                 var lat = latlngs[i].lat;
56                 var lng = latlngs[i].lng;
57                 var late5 = Math.floor(lat * 1e5);
58                 var lnge5 = Math.floor(lng * 1e5);
59                 dlat = late5 - plat;
60                 dlng = lnge5 - plng;
61                 plat = late5;
62                 plng = lnge5;
63                 encoded_points +=
64                         L.PolylineUtil.encodeSignedNumber(dlat) +
65                         L.PolylineUtil.encodeSignedNumber(dlng);
66         }
67         return encoded_points;
68 };
69
70 // This function is very similar to Google's, but I added
71 // some stuff to deal with the double slash issue.
72 L.PolylineUtil.encodeNumber = function (num) {
73         var encodeString = "";
74         var nextValue, finalValue;
75         while (num >= 0x20) {
76                 nextValue = (0x20 | (num & 0x1f)) + 63;
77                 encodeString += (String.fromCharCode(nextValue));
78                 num >>= 5;
79         }
80         finalValue = num + 63;
81         encodeString += (String.fromCharCode(finalValue));
82         return encodeString;
83 };
84
85 // This one is Google's verbatim.
86 L.PolylineUtil.encodeSignedNumber = function (num) {
87         var sgn_num = num << 1;
88         if (num < 0) {
89                 sgn_num = ~(sgn_num);
90         }
91         return (L.PolylineUtil.encodeNumber(sgn_num));
92 };
93
94 L.PolylineUtil.decode = function (encoded) {
95         var len = encoded.length;
96         var index = 0;
97         var latlngs = [];
98         var lat = 0;
99         var lng = 0;
100
101         while (index < len) {
102                 var b;
103                 var shift = 0;
104                 var result = 0;
105                 do {
106                         b = encoded.charCodeAt(index++) - 63;
107                         result |= (b & 0x1f) << shift;
108                         shift += 5;
109                 } while (b >= 0x20);
110                 var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
111                 lat += dlat;
112
113                 shift = 0;
114                 result = 0;
115                 do {
116                         b = encoded.charCodeAt(index++) - 63;
117                         result |= (b & 0x1f) << shift;
118                         shift += 5;
119                 } while (b >= 0x20);
120                 var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
121                 lng += dlng;
122
123                 latlngs.push(new L.LatLng(lat * 1e-5, lng * 1e-5));
124         }
125
126         return latlngs;
127 };