-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathleaflet.svgicon.js
96 lines (74 loc) · 2.34 KB
/
leaflet.svgicon.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
(function (window, document, undefined) {
L.SVGIcon = L.Icon.extend({
options: {
iconSize: [24, 24]
},
_createIcon: function (name, oldIcon) {
var src = this._getIconUrl(name);
if (!src) {
if (name === 'icon') {
throw new Error('iconUrl not set in Icon options (see the docs).');
}
return null;
}
var img;
if (!oldIcon || oldIcon.tagName !== 'OBJECT') {
img = this._createImg(src);
} else {
img = this._createImg(src, oldIcon);
}
this._setIconStyles(img, name);
return img;
},
_setIconStyles: function (img, name) {
var options = this.options,
size = L.point(options[name + 'Size']),
anchor;
anchor = L.point(options.iconAnchor);
if (!anchor && size) {
anchor = size.divideBy(2, true);
}
img.className = 'leaflet-marker-' + name + ' ' + options.className;
img.addEventListener('load', this._onload, true);
if (anchor) {
img.style.marginLeft = (-anchor.x) + 'px';
img.style.marginTop = (-anchor.y) + 'px';
}
if (size) {
img.style.width = size.x + 'px';
img.style.height = size.y + 'px';
}
img.options = options;
},
_createImg: function (src, el) {
el = el || document.createElement('object');
el.type = "image/svg+xml";
el.data = src;
return el;
},
_getIconUrl: function (name) {
return this.options[name + 'Url'];
},
_onload: function (e) {
var options = this.options;
var svgDoc = this.contentDocument;
if (options.foregroundColor) {
var paths = svgDoc.getElementsByTagName('path');
for (var i=paths.length-1; i>=0; i--) {
var pp = paths[i];
pp.style.fill = options.foregroundColor;
}
}
if (options.backgroundColor) {
var circles = svgDoc.getElementsByTagName('circle');
for (var i=circles.length-1; i>=0; i--) {
var cc = circles[i];
cc.style.fill = options.backgroundColor;
}
}
}
});
L.svgIcon = function (options) {
return new L.SVGIcon(options);
};
}(this, document));