-
Notifications
You must be signed in to change notification settings - Fork 2
/
setStyleWithLabel.js
37 lines (26 loc) · 1.04 KB
/
setStyleWithLabel.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
(function () {
"use strict";
ol.Feature.prototype.setStyleWithLabel = function (style) {
if (!style || !style.getText || !style.getText()) {
return ol.Feature.prototype.setStyle.apply(this, [style]);
}
var clonedStyle = style.clone();
var createLabel = function (tpl, data) {
return tpl.replace(/\${([^}]+)}/g, function (match, p1) {
return data[p1] || "";
});
};
var labelStyle = new ol.style.Style({
text: clonedStyle.getText().clone()
});
labelStyle.getText().setText(labelStyle.getText().getText());
var newstyle = function () {
var feature = this;
var label = createLabel(labelStyle.getText().getText() || '', feature.get("data") || {});
labelStyle.getText().setText(label);
clonedStyle.getText().setText(undefined);
return [clonedStyle, labelStyle];
};
return ol.Feature.prototype.setStyle.apply(this, [newstyle]);
};
})();