-
Notifications
You must be signed in to change notification settings - Fork 0
/
Control.Nanoscale.js
78 lines (59 loc) · 2.16 KB
/
Control.Nanoscale.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
L.Control.Nanoscale = L.Control.extend({
options: {
position: 'topright',
maxWidth: 300,
updateWhenIdle: false,
nanometersPerPixel: 1000,
/*ratioAtZoom: undefined,*/
},
onAdd: function (map) {
this._map = map;
var className = 'leaflet-control-scale',
container = L.DomUtil.create('div', className),
options = this.options;
this._addScales(options, className, container);
map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
map.whenReady(this._update, this);
return container;
},
onRemove: function (map) {
map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
},
_addScales: function (options, className, container) {
this._scale = L.DomUtil.create('div', className + '-line', container);
},
_update: function () {
var options = this.options,
bounds = this._map.getBounds(),
maxZoom = options.ratioAtZoom !== undefined ? options.ratioAtZoom :
this._map.getMaxZoom(),
dist = (this._map.project(bounds.getNorthEast(), maxZoom).x -
this._map.project(bounds.getSouthWest(), maxZoom).x),
size = this._map.getSize(),
maxNanometers = 0;
if (size.x > 0) {
maxNanometers = dist * (options.maxWidth / size.x) * options.nanometersPerPixel;
}
this._updateScales(options, maxNanometers);
},
_updateScales: function (options, maxNanometers) {
var nanometers = this._getRoundNum(maxNanometers);
this._scale.style.width = this._getScaleWidth(nanometers / maxNanometers) + 'px';
this._scale.innerHTML =
nanometers < 1000 ? nanometers + ' nm' :
nanometers < 1000000 ? (nanometers / 1000) + ' \u00b5m' :
(nanometers / 1000000) + ' mm';
},
_getScaleWidth: function (ratio) {
return Math.round(this.options.maxWidth * ratio) - 10;
},
_getRoundNum: function (num) {
var pow10 = Math.pow(10, (Math.floor(num) + '').length - 1),
d = num / pow10;
d = d >= 10 ? 10 : d >= 5 ? 5 : d >= 3 ? 3 : d >= 2 ? 2 : 1;
return pow10 * d;
}
});
L.control.nanoscale = function (options) {
return new L.Control.Nanoscale(options);
};