Conditions | 24 |
Total Lines | 345 |
Code Lines | 222 |
Lines | 345 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like script.js ➔ createMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
72 | function createMap(mapOpts, poi) { |
||
73 | |||
74 | // const mapOpts = olMapData[0].mapOpts; |
||
75 | // const poi = olMapData[0].poi; |
||
76 | |||
77 | if (!olEnable) { |
||
78 | return; |
||
79 | } |
||
80 | if (!olTestCSSsupport()) { |
||
81 | olEnable = false; |
||
82 | return; |
||
83 | } |
||
84 | |||
85 | // find map element location |
||
86 | var cleartag = document.getElementById(mapOpts.id + '-clearer'); |
||
87 | if (cleartag === null) { |
||
88 | return; |
||
89 | } |
||
90 | // create map element and add to document |
||
91 | var fragment = olCreateMaptag(mapOpts.id, mapOpts.width, mapOpts.height); |
||
92 | cleartag.parentNode.insertBefore(fragment, cleartag); |
||
93 | |||
94 | |||
95 | |||
96 | const overlayGroup = new ol.layer.Group({title: 'Overlays', fold: 'open', layers: []}); |
||
97 | const baseLyrGroup = new ol.layer.Group({'title': 'Base maps', layers: []}); |
||
98 | |||
99 | const map = new ol.Map({ |
||
100 | target: document.getElementById('map'), |
||
101 | layers: [baseLyrGroup, overlayGroup], |
||
102 | view: new ol.View({ |
||
103 | center: ol.proj.transform([mapOpts.lon, mapOpts.lat], 'EPSG:4326', 'EPSG:3857'), |
||
104 | zoom: mapOpts.zoom, |
||
105 | projection: 'EPSG:3857' |
||
106 | }) |
||
107 | }); |
||
108 | |||
109 | let /** |
||
110 | * Thunderforest API key. |
||
111 | * |
||
112 | * @type {String} |
||
113 | */ |
||
114 | tfApiKey = ''; |
||
115 | let /** |
||
116 | * OSM tiles flag. |
||
117 | * |
||
118 | * @type {Boolean} |
||
119 | */ |
||
120 | osmEnable = true; |
||
121 | if (osmEnable) { |
||
122 | baseLyrGroup.getLayers().push( |
||
123 | new ol.layer.Tile({ |
||
124 | visible: true, |
||
125 | title: 'OSM', |
||
126 | type: 'base', |
||
127 | source: new ol.source.OSM() |
||
128 | })); |
||
129 | |||
130 | baseLyrGroup.getLayers().push( |
||
131 | new ol.layer.Tile({ |
||
132 | visible: mapOpts.baselyr === "cycle map", |
||
133 | title: 'cycle map', |
||
134 | type: 'base', |
||
135 | source: new ol.source.OSM({ |
||
136 | url: 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
137 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
138 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
139 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
140 | }) |
||
141 | })); |
||
142 | |||
143 | baseLyrGroup.getLayers().push( |
||
144 | new ol.layer.Tile({ |
||
145 | visible: mapOpts.baselyr === "transport", |
||
146 | title: 'transport', |
||
147 | type: 'base', |
||
148 | source: new ol.source.OSM({ |
||
149 | url: 'https://{a-c}.tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
150 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
151 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
152 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
153 | }) |
||
154 | })); |
||
155 | |||
156 | baseLyrGroup.getLayers().push( |
||
157 | new ol.layer.Tile({ |
||
158 | visible: mapOpts.baselyr === "landscape", |
||
159 | title: 'landscape', |
||
160 | type: 'base', |
||
161 | source: new ol.source.OSM({ |
||
162 | url: 'https://{a-c}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
163 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
164 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
165 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
166 | }) |
||
167 | })); |
||
168 | |||
169 | baseLyrGroup.getLayers().push( |
||
170 | new ol.layer.Tile({ |
||
171 | visible: mapOpts.baselyr === "outdoors", |
||
172 | title: 'outdoors', |
||
173 | type: 'base', |
||
174 | source: new ol.source.OSM({ |
||
175 | url: 'https://{a-c}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
176 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
177 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
178 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
179 | }) |
||
180 | })); |
||
181 | } |
||
182 | |||
183 | let /** |
||
184 | * Bing tiles flag. |
||
185 | * |
||
186 | * @type {Boolean} |
||
187 | */ |
||
188 | bEnable = false; |
||
189 | let /** |
||
190 | * Bing API key. |
||
191 | * |
||
192 | * @type {String} |
||
193 | */ |
||
194 | bApiKey = ''; |
||
195 | if (bEnable && bApiKey !== '') { |
||
196 | baseLyrGroup.getLayers().push( |
||
197 | new ol.layer.Tile({ |
||
198 | visible: mapOpts.baselyr === "bing road", |
||
199 | title: 'bing road', |
||
200 | type: 'base', |
||
201 | source: new ol.source.BingMaps({ |
||
202 | key: bApiKey, |
||
203 | imagerySet: 'Road' |
||
204 | }) |
||
205 | })); |
||
206 | |||
207 | baseLyrGroup.getLayers().push( |
||
208 | new ol.layer.Tile({ |
||
209 | visible: mapOpts.baselyr === "bing sat", |
||
210 | title: 'bing sat', |
||
211 | type: 'base', |
||
212 | source: new ol.source.BingMaps({ |
||
213 | key: bApiKey, |
||
214 | imagerySet: 'Aerial' |
||
215 | }) |
||
216 | })); |
||
217 | |||
218 | baseLyrGroup.getLayers().push( |
||
219 | new ol.layer.Tile({ |
||
220 | visible: mapOpts.baselyr === "bing hybrid", |
||
221 | title: 'bing hybrid', |
||
222 | type: 'base', |
||
223 | source: new ol.source.BingMaps({ |
||
224 | key: bApiKey, |
||
225 | imagerySet: 'AerialWithLabels' |
||
226 | }) |
||
227 | })); |
||
228 | } |
||
229 | |||
230 | let /** |
||
231 | * Stamen tiles flag. |
||
232 | * |
||
233 | * @type {Boolean} |
||
234 | */ |
||
235 | stamenEnable = false; |
||
236 | if (stamenEnable) { |
||
237 | baseLyrGroup.getLayers().push( |
||
238 | new ol.layer.Tile({ |
||
239 | visible: false, |
||
240 | type: 'base', |
||
241 | title: 'toner', |
||
242 | source: new ol.source.Stamen({layer: 'toner'}) |
||
243 | }) |
||
244 | ); |
||
245 | baseLyrGroup.getLayers().push( |
||
246 | new ol.layer.Tile({ |
||
247 | visible: false, |
||
248 | type: 'base', |
||
249 | title: 'terrain', |
||
250 | source: new ol.source.Stamen({layer: 'terrain'}) |
||
251 | }) |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | map.addControl(new ol.control.ScaleLine({bar: true, text: true})); |
||
256 | map.addControl(new ol.control.MousePosition({ |
||
257 | coordinateFormat: ol.coordinate.createStringXY(4), projection: 'EPSG:4326', |
||
258 | })); |
||
259 | map.addControl(new ol.control.FullScreen()); |
||
260 | map.addControl(new ol.control.OverviewMap()); |
||
261 | map.addControl(new ol.control.LayerSwitcher()); |
||
262 | |||
263 | const iconScale = 1.5; |
||
264 | const vectorSource = new ol.source.Vector(); |
||
265 | poi.forEach((p) => { |
||
266 | const f = new ol.Feature({ |
||
267 | geometry: new ol.geom.Point(ol.proj.fromLonLat([p.lon, p.lat])), |
||
268 | description: p.txt, |
||
269 | img: p.img, |
||
270 | rowId: p.rowId, |
||
271 | lat: p.lat, |
||
272 | lon: p.lon, |
||
273 | angle: p.angle, |
||
274 | alt: p.img.substring(0, p.img.lastIndexOf(".")) |
||
275 | }); |
||
276 | f.setId(p.rowId); |
||
277 | f.setStyle(new ol.style.Style({ |
||
278 | text: new ol.style.Text({ |
||
279 | text: "" + p.rowId, |
||
280 | textAlign: 'left', |
||
281 | textBaseline: 'bottom', |
||
282 | offsetX: 8, |
||
283 | offsetY: -8, |
||
284 | scale: iconScale, |
||
285 | fill: new ol.style.Fill({color: 'rgb(0,0,0)'}), |
||
286 | font: '12px monospace bold', |
||
287 | backgroundFill: new ol.style.Fill({color: 'rgba(255,255,255,.4)'}) |
||
288 | }), image: new ol.style.Icon({ |
||
289 | src: "./" + p.img, crossOrigin: '', opacity: p.opacity, scale: iconScale, rotation: p.angle * Math.PI / 180, |
||
290 | }), |
||
291 | })); |
||
292 | vectorSource.addFeature(f); |
||
293 | }); |
||
294 | |||
295 | overlayGroup.getLayers().push(new ol.layer.Vector({title: 'POI', visible: true, source: vectorSource})); |
||
296 | |||
297 | if (mapOpts.kmlfile.length > 0) { |
||
298 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
299 | title: 'KML file', visible: true, |
||
300 | source: new ol.source.VectorSource({ |
||
301 | url: mapOpts.kmlfile, |
||
302 | format: new ol.format.KML(), |
||
303 | }), |
||
304 | style: {label: "${name}"} |
||
305 | })); |
||
306 | } |
||
307 | |||
308 | if (mapOpts.geojsonfile.length > 0) { |
||
309 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
310 | title: 'GeoJSON file', visible: true, |
||
311 | source: new ol.source.VectorSource({ |
||
312 | url: mapOpts.geojsonfile, |
||
313 | format: new ol.format.GeoJSON(), |
||
314 | }), |
||
315 | style: { |
||
316 | /* TODO */ |
||
317 | strokeColor: "#FF00FF", |
||
318 | strokeWidth: 3, |
||
319 | strokeOpacity: 0.7, |
||
320 | pointRadius: 4, |
||
321 | fillColor: "#FF99FF", |
||
322 | fillOpacity: 0.7 |
||
323 | } |
||
324 | })); |
||
325 | } |
||
326 | |||
327 | if (mapOpts.gpxfile.length > 0) { |
||
328 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
329 | title: 'GPS track', visible: true, |
||
330 | source: new ol.source.VectorSource({ |
||
331 | url: mapOpts.gpxfile, |
||
332 | format: new ol.format.GPX(), |
||
333 | }), |
||
334 | style: { |
||
335 | /* TODO */ |
||
336 | strokeColor: "#0000FF", |
||
337 | strokeWidth: 3, |
||
338 | strokeOpacity: 0.7, |
||
339 | pointRadius: 4, |
||
340 | fillColor: "#0099FF", |
||
341 | fillOpacity: 0.7 |
||
342 | } |
||
343 | })); |
||
344 | } |
||
345 | |||
346 | const container = document.getElementById('popup'); |
||
347 | const content = document.getElementById('popup-content'); |
||
348 | const closer = document.getElementById('popup-closer'); |
||
349 | |||
350 | const overlay = new ol.Overlay({ |
||
351 | element: container, autoPan: true, autoPanAnimation: { |
||
352 | duration: 250, |
||
353 | }, //stopEvent: false, |
||
354 | }); |
||
355 | map.addOverlay(overlay); |
||
356 | |||
357 | /** |
||
358 | * Add a click handler to hide the popup. |
||
359 | * @return {boolean} Don't follow the href. |
||
360 | */ |
||
361 | closer.onclick = function () { |
||
362 | overlay.setPosition(undefined); |
||
363 | closer.blur(); |
||
364 | return false; |
||
365 | }; |
||
366 | |||
367 | // display popup on click |
||
368 | map.on('singleclick', function (evt) { |
||
369 | const selFeature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { |
||
370 | return feature; |
||
371 | }); |
||
372 | if (selFeature) { |
||
373 | overlay.setPosition(evt.coordinate); |
||
374 | |||
375 | let pContent = '<div class="spacer"> </div>'; |
||
376 | let locDesc = ''; |
||
377 | |||
378 | if (selFeature.get('rowId') !== undefined) { |
||
379 | pContent += '<span class="rowId">' + selFeature.get('rowId') + ': </span>'; |
||
380 | } |
||
381 | if (selFeature.get('name') !== undefined) { |
||
382 | pContent += '<span class="txt">' + selFeature.get('name') + '</span>'; |
||
383 | locDesc = selFeature.get('name'); |
||
384 | // TODO strip <p> tag from locDesc |
||
385 | // locDesc = selFeature.get('name').split(/\s+/).slice(0,2).join('+'); |
||
386 | } |
||
387 | if (selFeature.get('ele') !== undefined) { |
||
388 | pContent += '<div class="ele">elevation: ' + selFeature.get('ele') + '</div>'; |
||
389 | } |
||
390 | if (selFeature.get('type') !== undefined) { |
||
391 | pContent += '<div>' + selFeature.get('type') + '</div>'; |
||
392 | } |
||
393 | if (selFeature.get('time') !== undefined) { |
||
394 | pContent += '<div class="time">time: ' + selFeature.get('time') + '</div>'; |
||
395 | } |
||
396 | if (selFeature.get('description') !== undefined) { |
||
397 | pContent += '<div class="desc">' + selFeature.get('description') + '</div>'; |
||
398 | } |
||
399 | if (selFeature.get('img') !== undefined) { |
||
400 | pContent += '<div class="coord" title="lat;lon">' + '<img src="' + selFeature.get('img') + '" width="16" height="16" ' + 'style="transform:rotate(' + selFeature.get('angle') + 'deg)" /> ' + '<a href="geo:' + selFeature.get('lat') + ',' + selFeature.get('lon') + '?q=' + selFeature.get('lat') + ',' + selFeature.get('lon') + '(' + selFeature.get('alt') + ')" title="Open in navigation app">' + ol.coordinate.format([selFeature.get('lon'), selFeature.get('lat')], '{x}º; {y}º', 4) + '</a></div>'; |
||
401 | } |
||
402 | content.innerHTML = pContent; |
||
403 | } else { |
||
404 | // do nothing?... |
||
405 | } |
||
406 | }); |
||
407 | |||
408 | // change mouse cursor when over marker |
||
409 | map.on('pointermove', function (e) { |
||
410 | const pixel = map.getEventPixel(e.originalEvent); |
||
411 | const hit = map.hasFeatureAtPixel(pixel); |
||
412 | map.getTarget().style.cursor = hit ? 'pointer' : ''; |
||
413 | }); |
||
414 | |||
415 | return map; |
||
416 | } |
||
417 | |||
540 |