-
Notifications
You must be signed in to change notification settings - Fork 34
/
Leaflet.MultiOptionsPolyline.js
114 lines (92 loc) · 3.47 KB
/
Leaflet.MultiOptionsPolyline.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(["leaflet"], factory);
} else if (typeof module === "object" && module.exports) {
factory(require("leaflet"));
} else {
factory(root.L);
}
} (this, function (L) {
"use strict";
/*
* L.MultiOptionsPolyline is a MultiPolyLine which parts can be styled differently.
* options: {
* multiOptions: {
* optionIdxFn: function (latLng, prevLatLng, index, allLatlngs),
* fnContext: ctx, // the context to call optionIdxFn (optional)
* options: [{}, {}, {}] or function, // options for the index returned by optionIdxFn. If supplied with a function then it will be called with the index
* copyBaseOptions: true
* },
* // other options from Polyline
* }
*/
var MultiOptionsPolyline = L.FeatureGroup.extend({
initialize: function (latlngs, options) {
var copyBaseOptions = options.multiOptions.copyBaseOptions;
this._layers = {};
this._options = options;
if (copyBaseOptions === undefined || copyBaseOptions) {
this._copyBaseOptions();
}
this.setLatLngs(latlngs);
},
_copyBaseOptions: function () {
var multiOptions = this._options.multiOptions,
baseOptions,
optionsArray = multiOptions.options,
i, len = optionsArray.length;
baseOptions = L.extend({}, this._options);
delete baseOptions.multiOptions;
for (i = 0; i < len; ++i) {
optionsArray[i] = L.extend({}, baseOptions, optionsArray[i]);
}
},
setLatLngs: function (latlngs) {
var i, len = latlngs.length,
multiOptions = this._options.multiOptions,
optionIdxFn = multiOptions.optionIdxFn,
fnContext = multiOptions.fnContext || this,
prevOptionIdx, optionIdx,
segmentLatlngs;
this._originalLatlngs = latlngs;
this.eachLayer(function (layer) {
this.removeLayer(layer);
}, this);
for (i = 1; i < len; ++i) {
optionIdx = optionIdxFn.call(
fnContext, latlngs[i], latlngs[i - 1], i, latlngs);
if (i === 1) {
segmentLatlngs = [latlngs[0]];
prevOptionIdx = optionIdxFn.call(fnContext, latlngs[0], latlngs[0], 0, latlngs);
}
segmentLatlngs.push(latlngs[i]);
// is there a change in options or is it the last point?
if (prevOptionIdx !== optionIdx || i === len - 1) {
// Check if options is a function or an array
if (typeof multiOptions.options === "function") {
this.addLayer(L.polyline(segmentLatlngs, multiOptions.options(prevOptionIdx)));
} else {
this.addLayer(L.polyline(segmentLatlngs, multiOptions.options[prevOptionIdx]));
}
prevOptionIdx = optionIdx;
segmentLatlngs = [latlngs[i]];
}
}
return this;
},
getLatLngs: function () {
return this._originalLatlngs;
},
getLatLngsSegments: function () {
var latlngs = [];
this.eachLayer(function (layer) {
latlngs.push(layer.getLatLngs());
});
return latlngs;
}
});
L.MultiOptionsPolyline = MultiOptionsPolyline;
L.multiOptionsPolyline = function (latlngs, options) {
return new MultiOptionsPolyline(latlngs, options);
};
}));