-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathL.Control.Basemaps.js
executable file
·150 lines (132 loc) · 5.61 KB
/
L.Control.Basemaps.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
L.Control.Basemaps = L.Control.extend({
_map: null,
includes: L.Evented ? L.Evented.prototype : L.Mixin.Event,
options: {
position: "bottomright",
tileX: 0,
tileY: 0,
tileZ: 0,
layers: [] // list of basemap layer objects, first in list is default and added to map with this control
},
basemap: null,
onAdd: function(map) {
this._map = map;
var container = L.DomUtil.create("div", "basemaps leaflet-control closed");
// disable events
L.DomEvent.disableClickPropagation(container);
if (!L.Browser.touch) {
L.DomEvent.disableScrollPropagation(container);
}
this.options.basemaps.forEach(function(d, i) {
var basemapClass = "basemap";
if (i === 0) {
this.basemap = d;
this._map.addLayer(d);
basemapClass += " active";
} else if (i === 1) {
basemapClass += " alt";
}
var url;
if (d.options.iconURL) {
url = d.options.iconURL;
} else {
var coords = { x: this.options.tileX, y: this.options.tileY };
url = L.Util.template(
d._url,
L.extend(
{
s: d._getSubdomain(coords),
x: coords.x,
y: d.options.tms ? d._globalTileRange.max.y - coords.y : coords.y,
z: this.options.tileZ
},
d.options
)
);
if (d instanceof L.TileLayer.WMS) {
// d may not yet be initialized, yet functions below expect ._map to be set
d._map = map;
// unfortunately, calling d.getTileUrl() does not work due to scope issues
// have to replicate some of the logic from L.TileLayer.WMS
// adapted from L.TileLayer.WMS::onAdd
var crs = d.options.crs || map.options.crs;
var wmsParams = L.extend({}, d.wmsParams);
var wmsVersion = parseFloat(wmsParams.version);
var projectionKey = wmsVersion >= 1.3 ? "crs" : "srs";
wmsParams[projectionKey] = crs.code;
// adapted from L.TileLayer.WMS::getTileUrl
var coords2 = L.point(coords);
coords2.z = this.options.tileZ;
var tileBounds = d._tileCoordsToBounds(coords2);
var nw = crs.project(tileBounds.getNorthWest());
var se = crs.project(tileBounds.getSouthEast());
var bbox = (wmsVersion >= 1.3 && crs === L.CRS.EPSG4326
? [se.y, nw.x, nw.y, se.x]
: [nw.x, se.y, se.x, nw.y]
).join(",");
url +=
L.Util.getParamString(wmsParams, url, d.options.uppercase) +
(d.options.uppercase ? "&BBOX=" : "&bbox=") +
bbox;
}
}
var basemapNode = L.DomUtil.create("div", basemapClass, container);
var imgNode = L.DomUtil.create("img", null, basemapNode);
imgNode.src = url;
if (d.options && d.options.label) {
imgNode.title = d.options.label;
}
L.DomEvent.on(
basemapNode,
"click",
function() {
// intercept open click on mobile devices and show options
if (this.options.basemaps.length > 2 && L.Browser.mobile) {
if (L.DomUtil.hasClass(container, "closed")) {
L.DomUtil.removeClass(container, "closed");
return;
}
}
//if different, remove previous basemap, and add new one
if (d != this.basemap) {
map.removeLayer(this.basemap);
map.addLayer(d);
d.bringToBack();
map.fire("baselayerchange", d);
this.basemap = d;
L.DomUtil.removeClass(container.getElementsByClassName("basemap active")[0], "active");
L.DomUtil.addClass(basemapNode, "active");
var altIdx = (i + 1) % this.options.basemaps.length;
L.DomUtil.removeClass(container.getElementsByClassName("basemap alt")[0], "alt");
L.DomUtil.addClass(container.getElementsByClassName("basemap")[altIdx], "alt");
L.DomUtil.addClass(container, "closed");
}
},
this
);
}, this);
if (this.options.basemaps.length > 2 && !L.Browser.mobile) {
L.DomEvent.on(
container,
"mouseenter",
function() {
L.DomUtil.removeClass(container, "closed");
},
this
);
L.DomEvent.on(
container,
"mouseleave",
function() {
L.DomUtil.addClass(container, "closed");
},
this
);
}
this._container = container;
return this._container;
}
});
L.control.basemaps = function(options) {
return new L.Control.Basemaps(options);
};