forked from MarcChasse/leaflet.ScaleFactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.scalefactor.js
81 lines (61 loc) · 2.68 KB
/
leaflet.scalefactor.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
(function (factory, window) {
// define an AMD module that relies on 'leaflet'
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
// define a Common JS module that relies on 'leaflet'
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
}
// attach your plugin to the global 'L' variable
if (typeof window !== 'undefined' && window.L) {
window.L.Control.ScaleFactor = factory(L);
L.control.scalefactor = function (options) {
return new window.L.Control.ScaleFactor(options);
};
}
}(function (L) {
var ScaleFactor = L.Control.extend({
options: {
position: 'bottomleft',
updateWhenIdle: true
},
onAdd: function (map) {
var className = 'leaflet-control-scalefactor',
container = L.DomUtil.create('div', className),
options = this.options;
this._mScale = L.DomUtil.create('div', className + '-line', 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);
},
_pxTOmm: (function () {
var heightRef = document.createElement('div');
heightRef.style = 'height:1mm;display:none';
heightRef.id = 'heightRef';
document.body.appendChild(heightRef);
heightRef = document.getElementById('heightRef');
// var pxPermm = $('#heightRef').height(); // requires jQuery >= 1.0.0
var pxPermm = parseFloat(getComputedStyle(heightRef, null).height.replace("px", ""))
heightRef.parentNode.removeChild(heightRef);
return function pxTOmm(px) {
return px / pxPermm;
}
})(),
_update: function () {
var map = this._map;
var CenterOfMap = map.getSize().y / 2;
var RealWorlMetersPer100Pixels = map.distance(
map.containerPointToLatLng([0, CenterOfMap]),
map.containerPointToLatLng([100, CenterOfMap])
);
var ScreenMetersPer100Pixels = this._pxTOmm(100) / 1000;
var scaleFactor = RealWorlMetersPer100Pixels / ScreenMetersPer100Pixels;
//.replace formats the scale with commas 50000 -> 50,000
this._mScale.innerHTML = '1:' + Math.round(scaleFactor).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
});
return ScaleFactor;
}, window));