/
gridSystem.js
95 lines (86 loc) · 4.58 KB
/
gridSystem.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
module.exports = function gridSystem(options) {
var map = options.map || document.getElementById("map") || L.map('map');
options.cellSize = options.cellSize || { rows:100, cols:100 };
require('leaflet-graticule');
// require('../Leaflet.Graticule.js');
options.graticuleOptions = options.graticuleOptions || {
showLabel: true,
zoomInterval: [
{start: 2, end: 2, interval: 100},
{start: 2, end: 5, interval: 10},
{start: 5, end: 9, interval: 1},
{start: 9, end: 12, interval: 0.1},
{start: 12, end: 15, interval: 0.01},
{start: 15, end: 20, interval: 0.001},
],
opacity: 1,
color: '#ff0000',
latFormatTickLabel: function(lat) {
var decimalPlacesAfterZero = 0;
lat = lat.toString();
for(i in this.zoomInterval) {
if(map.getZoom() >= this.zoomInterval[i].start && map.getZoom() <= this.zoomInterval[i].end && this.zoomInterval[i].interval < 1)
decimalPlacesAfterZero = (this.zoomInterval[i].interval + '').split('.')[1].length;
}
if (lat < 0) {
lat = lat * -1;
lat = lat.toString();
if(lat.indexOf(".") != -1) lat = lat.split('.')[0] + '.' + lat.split('.')[1].slice(0,decimalPlacesAfterZero);
return '' + lat + 'S';
}
else if (lat > 0) {
if(lat.indexOf(".") != -1) lat = lat.split('.')[0] + '.' + lat.split('.')[1].slice(0,decimalPlacesAfterZero)
return '' + lat + 'N';
}
return '' + lat;
},
lngFormatTickLabel: function(lng) {
var decimalPlacesAfterZero = 0;
lng = lng.toString();
for(i in this.zoomInterval) {
if(map.getZoom() >= this.zoomInterval[i].start && map.getZoom() <= this.zoomInterval[i].end && this.zoomInterval[i].interval < 1)
decimalPlacesAfterZero = (this.zoomInterval[i].interval + '').split('.')[1].length;
}
if (lng > 180) {
lng = 360 - lng;
lng = lng.toString();
if(lng.indexOf(".") != -1) lng = lng.split('.')[0] + '.' + lng.split('.')[1].slice(0,decimalPlacesAfterZero)
return '' + lng + 'W';
}
else if (lng > 0 && lng < 180) {
if(lng.indexOf(".") != -1) lng = lng.split('.')[0] + '.' + lng.split('.')[1].slice(0,decimalPlacesAfterZero)
return '' + lng + 'E';
}
else if (lng < 0 && lng > -180) {
lng = lng * -1;
lng = lng.toString();
if(lng.indexOf(".") != -1) lng = lng.split('.')[0] + '.' + lng.split('.')[1].slice(0,decimalPlacesAfterZero)
return '' + lng + 'W';
}
else if (lng == -180) {
lng = lng*-1;
if(lng.indexOf(".") != -1) lng = lng.split('.')[0] + '.' + lng.split('.')[1].slice(0,decimalPlacesAfterZero)
return '' + lng;
}
else if (lng < -180) {
lng = 360 + lng;
if(lng.indexOf(".") != -1) lng = lng.split('.')[0] + '.' + lng.split('.')[1].slice(0,decimalPlacesAfterZero)
return '' + lng + 'W';
}
else if(lng == 0) {
return '' + lng;
}
},
}
var layer = L.latlngGraticule(options.graticuleOptions).addTo(map);
function addGrid() {
layer = L.latlngGraticule(options.graticuleOptions).addTo(map);
}
function removeGrid() {
layer.remove();
}
return {
removeGrid: removeGrid,
addGrid: addGrid,
}
}