-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
240 lines (207 loc) · 7.83 KB
/
index.html
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Latitude/longitude of a point</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="style.css" />
<script src="https://d3js.org/d3.v5.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Inconsolata:400,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"
integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"
integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
crossorigin=""></script>
<script src="leaflet-hash.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol/dist/L.Control.Locate.min.css" />
<script src="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol/dist/L.Control.Locate.min.js" charset="utf-8"></script>
</head>
<body>
<div id='map'></div>
<div id='main-bar' class='bar leaflet-bar'>
<span class='template'>Latitude, longitude</span>:
<input type='text' id='latlng' name='latlng' />
</div>
<div id='templates' class='bar leaflet-bar hidden'>
</div>
<script>
// TODO:
// remember template
var map = L.map('map'),
t = {
"name": "latlong",
"title": "Latitude, longitude",
"template": "%lat%, %lng%"
},
disableTextChange = false;
map.setView([52.3674, 4.915], 6);
var hash = new L.Hash(map);
hash.addParameter('template');
L.control.locate({
drawCircle: false
}).addTo(map);
var tileUrl = 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';
var attribution = '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="https://carto.com/attributions">CARTO</a>';
var tileLayer = L.tileLayer(tileUrl, {
attribution: attribution,
minZoom: 2
}).addTo(map);
drawCrossHair();
map.on('move', function() {
updateLatLng();
});
d3.select('#main-bar span').on('click', function() {
d3.select('#templates').classed('hidden', !d3.select('#templates').classed('hidden'));
});
function textWidth(text) {
return text.length * 17.2 + 'px';
}
function validateLat(lat) {
if (lat >= -90 && lat <= 90) {
return lat;
} else {
return null;
}
}
function validateLng(lng) {
if (lng >= -180 && lng <= 180) {
return lng;
} else {
return null;
}
}
function setError(error) {
d3.select('#main-bar').classed('error', error);
}
map.on('zoomstart', function() {
d3.select('#templates').classed('hidden', true);
});
map.on('movestart', function() {
d3.select('#templates').classed('hidden', true);
});
map.on('moveend', function() {
disableInputEvent = false;
});
d3.select('input').on('input', function() {
var value = d3.select(this).property('value');
d3.select(this).style('width', textWidth(value));
var reNum = '\s*([-+]?[0-9]*\.?[0-9]*)\s*';
var reTemplate = RegExp.escape(t.template).replace(/%.*?%/g, reNum);
var value = d3.select('input').property('value');
var match = value.match(reTemplate);
// point, match must have length 3
var latlng;
if (match && match.length == 3) {
var order = t.template.indexOf('%lat%') < t.template.indexOf('%lng%');
var lat = validateLat(order ? match[1] : match[2]),
lng = validateLng(order ? match[2] : match[1]);
if (lat && lng) {
latlng = L.latLng(lat, lng);
}
}
if (latlng /*&& value == t.template.replace('%lat%', latlng.lat).replace('%lng%', latlng.lng)*/) {
disableTextChange = true;
map.panTo(latlng, {animate: false});
setError(false);
} else {
setError(true);
}
});
// From http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
RegExp.escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
};
function drawCrossHair() {
var crosshairLength = 10,
crosshair = [
[0, -1], [1, 0], [0, 1], [-1, 0]
];
var g = d3.select('.leaflet-control-container')
.append('div')
.attr('class', 'leaflet-middle')
.append('svg')
.attr('class', 'crosshair')
.attr('width', 64)
.attr('height', 64)
.append('svg:g')
.attr('transform', 'translate(32,32)')
.selectAll('line')
.data(crosshair);
g.enter().append('line')
.attr('class', 'crosshair-outline')
.attr('x1', function(d) { return d[0] * crosshairLength; })
.attr('y1', function(d) { return d[1] * crosshairLength; })
.attr('x2', function(d) { return d[0] * crosshairLength * 2; })
.attr('y2', function(d) { return d[1] * crosshairLength * 2; });
g.enter().append('line')
.attr('x1', function(d) { return d[0] * crosshairLength; })
.attr('y1', function(d) { return d[1] * crosshairLength; })
.attr('x2', function(d) { return d[0] * crosshairLength * 2; })
.attr('y2', function(d) { return d[1] * crosshairLength * 2; });
}
function templateToLatLng() {
var center = map.getCenter(),
zoom = map.getZoom(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
return t.template
.replace('%lat%', center.lat.toFixed(precision))
.replace('%lng%', ((center.lng + 180) % 360 - 180).toFixed(precision));
}
function updateLatLng() {
setError(false);
if (!disableTextChange) {
var latlng = templateToLatLng();
d3.select('input')
.property('value', latlng)
.style('width', textWidth(latlng));
}
disableTextChange = false;
}
d3.json('templates.json').then(function (json) {
console.log(json)
t = json.point[0];
var template = hash.getParameterValue('template');
if (template) {
for (var i = 0; i < json.point.length; i++) {
if (template === json.point[i].name) {
t = json.point[i];
d3.select('#main-bar span').html(t.title);
break;
}
}
}
updateLatLng();
d3.select('#templates')
.append('ul')
.selectAll('li')
.data(json.point)
.enter()
.append('li')
.classed('hidden', function(d, i) { return i == 0; })
.append('span')
.classed('template', true)
.html(function(d) { return d.title; })
.on('click', function(d) {
d3.select('#templates').classed('hidden', true);
t = d;
d3.select('#main-bar span').html(t.title);
hash.setParameterValue('template', d.name == 'latlong' ? null : d.name);
hash.onMapMove();
updateLatLng();
d3.selectAll('#templates li').classed('hidden', false);
d3.select(this.parentNode).classed('hidden', true);
});
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-4378752-1', 'bertspaan.nl');
ga('send', 'pageview');
</script>
</body>
</html>