-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathmap_label_control.js
More file actions
144 lines (117 loc) · 4.75 KB
/
map_label_control.js
File metadata and controls
144 lines (117 loc) · 4.75 KB
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
'use strict';
import { CanvasLayer } from '../external/L.CanvasLayer.js';
import Locations from "../model/Locations.js";
import { Position } from "../model/Position.js";
var MapLabelsCanvas = CanvasLayer.extend({
setData: function (data) {
this.needRedraw();
},
onDrawLayer: function (info) {
var zoom = this._map.getZoom();
var ctx = info.canvas.getContext('2d');
ctx.clearRect(0, 0, info.canvas.width, info.canvas.height);
ctx.textAlign = "center";
var self = this;
Locations.getLocations(function (locations) {
for (var i in locations) {
if (locations[i].position.z !== info.layer._map.plane) {
continue;
}
// Map textScale values to font sizes
let fontSize;
if (typeof locations[i].size === 'number') {
switch (locations[i].size) {
case 0:
fontSize = 0.08; // default
break;
case 1:
fontSize = 0.10; // medium
break;
case 2:
fontSize = 0.18; // large
break;
default:
fontSize = 0.08;
}
} else {
fontSize = 0.08; // fallback
}
// Convert textColor from decimal to hex, with fallback
let fontColour = 'white';
if (locations[i].color) {
fontColour = '#' + locations[i].color.toString(16).padStart(6, '0');
}
// Scale font size to match zoom
const fontSizeScaled = fontSize * Math.pow(2, zoom);
ctx.font = `bold ${fontSizeScaled}px Verdana`
ctx.fillStyle = fontColour
var position = locations[i].position;
var latLng = position.toCentreLatLng(self._map);
var canvasPoint = info.layer._map.latLngToContainerPoint(latLng);
const name = locations[i].name
// First split by <br> tags to handle explicit line breaks
const brLines = name.split('<br>')
const lines = []
brLines.forEach(brLine => {
const words = brLine.trim().split(' ')
let line = "";
words.forEach(word => {
if ((line + word).length < 10) {
if (line !== "") {
line += " "
}
line += word
} else {
if (line !== "") {
lines.push(line);
}
line = word;
}
})
if (line !== "") {
lines.push(line);
}
})
let y = canvasPoint.y;
lines.forEach(line => {
ctx.strokeText(line, canvasPoint.x, y);
ctx.fillText(line, canvasPoint.x, y);
y += (fontSize + 0.02) * Math.pow(2, zoom);
})
}
});
}
});
export var MapLabelControl = L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function (map) {
map.createPane("map-labels");
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control noselect');
container.style.background = 'none';
container.style.width = '100px';
container.style.height = 'auto';
var labelsButton = L.DomUtil.create('a', 'leaflet-bar leaflet-control leaflet-control-custom', container);
labelsButton.id = 'toggle-map-labels';
labelsButton.innerHTML = 'Toggle Labels';
L.DomEvent.on(labelsButton, 'click', this._toggleMapLabels, this);
this._enabled = true;
L.DomEvent.disableClickPropagation(container);
this._mapLabelsCanvas = new MapLabelsCanvas({ pane: "map-labels" });
this._map.addLayer(this._mapLabelsCanvas);
map.on('planeChanged', function () {
this._mapLabelsCanvas.drawLayer();
}, this);
return container;
},
_toggleMapLabels: function () {
if (this._enabled) {
this._map.getPane("map-labels").style.display = "none";
this._enabled = false;
} else {
this._map.getPane("map-labels").style.display = "";
this._enabled = true;
}
}
});