Passed
Pull Request — master (#10)
by Mark
07:24
created

ol6/script.js   A

Complexity

Total Complexity 20
Complexity/F 4

Size

Lines of Code 228
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 145
mnd 15
bc 15
fnc 5
dl 0
loc 228
rs 10
bpm 3
cpm 4
noi 11
c 0
b 0
f 0
1
const mapOpts = olMapData[0].mapOpts;
0 ignored issues
show
Bug introduced by
The variable olMapData seems to be never declared. If this is a global, consider adding a /** global: olMapData */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
const poi = olMapData[0].poi;
3
4
const overlayGroup = new ol.layer.Group({
0 ignored issues
show
Bug introduced by
The variable ol seems to be never declared. If this is a global, consider adding a /** global: ol */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
5
    title: 'Overlays', fold: 'open', layers: []
6
})
7
8
const baseLyrGroup = new ol.layer.Group({
9
    'title': 'Base maps',
10
    layers: [     ]
11
});
12
13
const map = new ol.Map({
14
    target: document.getElementById('map'),
15
    layers: [baseLyrGroup, overlayGroup],
16
    view: new ol.View({
17
        center:     ol.proj.transform([mapOpts.lon, mapOpts.lat], 'EPSG:4326', 'EPSG:3857'),
18
        zoom:       mapOpts.zoom,
19
        projection: 'EPSG:3857'
20
    })
21
});
22
23
if(osmEnable){
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable osmEnable is declared in the current environment, consider using typeof osmEnable === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
24
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: true, title: 'OSM', type: 'base', source: new ol.source.OSM()}));
25
26
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "cycle map", title: 'cycle map', type: 'base', source: new ol.source.OSM({
27
            url:'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey='+tfApiKey,
0 ignored issues
show
Bug introduced by
The variable tfApiKey seems to be never declared. If this is a global, consider adding a /** global: tfApiKey */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
28
            attributions: 'Data &copy;ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, '
29
                    + 'Tiles &copy;<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>'
30
                    + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>'
31
    })}));
32
33
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "transport", title: 'transport', type: 'base', source: new ol.source.OSM({
34
            url:'https://{a-c}.tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey='+tfApiKey,
35
            attributions: 'Data &copy;ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, '
36
                    + 'Tiles &copy;<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>'
37
                    + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>'
38
        })}));
39
40
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "landscape", title: 'landscape', type: 'base', source: new ol.source.OSM({
41
            url:'https://{a-c}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey='+tfApiKey,
42
            attributions: 'Data &copy;ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, '
43
                    + 'Tiles &copy;<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>'
44
                    + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>'
45
        })}));
46
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "outdoors", title: 'outdoors', type: 'base', source: new ol.source.OSM({
47
            url:'https://{a-c}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey='+tfApiKey,
48
            attributions: 'Data &copy;ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, '
49
                              + 'Tiles &copy;<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>'
50
                              + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>'
51
        })}));
52
53
}
54
55
if (bEnable && bApiKey !== '') {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable bEnable is declared in the current environment, consider using typeof bEnable === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Bug introduced by
The variable bApiKey seems to be never declared. If this is a global, consider adding a /** global: bApiKey */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
56
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "bing road", title: 'bing road', type: 'base', source: new ol.source.BingMaps({
57
            key: bApiKey,
58
            imagerySet: 'Road'
59
        })}));
60
61
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "bing sat", title: 'bing sat', type: 'base', source: new ol.source.BingMaps({
62
            key: bApiKey,
63
            imagerySet: 'Aerial'
64
        })}));
65
66
    baseLyrGroup.getLayers().push(new ol.layer.Tile({visible: mapOpts.baselyr === "bing hybrid", title: 'bing hybrid', type: 'base', source: new ol.source.BingMaps({
67
            key: bApiKey,
68
            imagerySet: 'AerialWithLabels'
69
        })}));
70
}
71
72
if (stamenEnable) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable stamenEnable is declared in the current environment, consider using typeof stamenEnable === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
73
    baseLyrGroup.getLayers().push(
74
        new ol.layer.Tile({visible: false, type: 'base', title: 'toner', source: new ol.source.Stamen({layer: 'toner'})})
75
    );
76
    baseLyrGroup.getLayers().push(
77
        new ol.layer.Tile({visible: false, type: 'base', title: 'terrain', source: new ol.source.Stamen({layer: 'terrain'})})
78
    );
79
}
80
81
map.addControl(new ol.control.ScaleLine({bar: true, text: true}));
82
map.addControl(new ol.control.MousePosition({
83
    coordinateFormat: ol.coordinate.createStringXY(4), projection: 'EPSG:4326',
84
}));
85
map.addControl(new ol.control.FullScreen());
86
map.addControl(new ol.control.OverviewMap());
87
88
89
const iconScale = 1.5;
90
const vectorSource = new ol.source.Vector();
91
poi.forEach((p) => {
92
    const f = new ol.Feature({
0 ignored issues
show
Bug introduced by
The variable ol seems to be never declared. If this is a global, consider adding a /** global: ol */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
93
        geometry:    new ol.geom.Point(ol.proj.fromLonLat([p.lon, p.lat])),
94
        description: p.txt,
95
        img:         p.img,
96
        rowId:       p.rowId,
97
        lat:         p.lat,
98
        lon:         p.lon,
99
        angle:       p.angle,
100
        alt:         p.img.substring(0, p.img.lastIndexOf("."))
101
    });
102
    f.setId(p.rowId);
103
    f.setStyle(new ol.style.Style({
104
        text:      new ol.style.Text({
105
            text:           "" + p.rowId,
106
            textAlign:      'left',
107
            textBaseline:   'bottom',
108
            offsetX:        8,
109
            offsetY:        -8,
110
            scale:          iconScale,
111
            fill:           new ol.style.Fill({color: 'rgb(0,0,0)'}),
112
            font:           '12px monospace bold',
113
            backgroundFill: new ol.style.Fill({color: 'rgba(255,255,255,.4)'})
114
        }), image: new ol.style.Icon({
115
            src: "./" + p.img, crossOrigin: '', opacity: p.opacity, scale: iconScale, rotation: p.angle * Math.PI / 180,
116
        }),
117
    }));
118
    vectorSource.addFeature(f);
119
});
120
121
overlayGroup.getLayers().push(new ol.layer.Vector({
122
    title: 'POI', visible: true, source: vectorSource
123
}));
124
125
map.addControl(new ol.control.LayerSwitcher());
126
127
if (mapOpts.kmlfile.length > 0) {
128
    overlayGroup.getLayers().push(new ol.layer.Vector({
129
        title: 'KML file', visible: true,
130
        source: new ol.source.VectorSource({
131
            url: mapOpts.kmlfile,
132
            format: new ol.format.KML(),
133
        }),
134
    }));
135
}
136
137
if (mapOpts.geojsonfile.length > 0) {
138
    overlayGroup.getLayers().push(new ol.layer.Vector({
139
        title: 'GeoJSON file', visible: true,
140
        source: new ol.source.VectorSource({
141
            url: mapOpts.geojsonfile,
142
            format: new ol.format.GeoJSON(),
143
        }),
144
        style: {
145
            /* TODO */
146
        }
147
    }));
148
}
149
150
if (mapOpts.gpxfile.length > 0) {
151
    overlayGroup.getLayers().push(new ol.layer.Vector({
152
        title: 'GPS track', visible: true,
153
        source: new ol.source.VectorSource({
154
            url: mapOpts.gpxfile,
155
            format: new ol.format.GPX(),
156
        }),
157
        style: {/* TODO */}
158
    }));
159
}
160
161
const container = document.getElementById('popup');
162
const content = document.getElementById('popup-content');
163
const closer = document.getElementById('popup-closer');
164
165
const overlay = new ol.Overlay({
166
    element: container, autoPan: true, autoPanAnimation: {
167
        duration: 250,
168
    }, //stopEvent: false,
169
});
170
map.addOverlay(overlay);
171
172
/**
173
 * Add a click handler to hide the popup.
174
 * @return {boolean} Don't follow the href.
175
 */
176
closer.onclick = function () {
177
    overlay.setPosition(undefined);
178
    closer.blur();
179
    return false;
180
};
181
182
// display popup on click
183
map.on('singleclick', function (evt) {
184
    const selFeature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
185
        return feature;
186
    });
187
    if (selFeature) {
188
        overlay.setPosition(evt.coordinate);
189
190
        let pContent = '<div class="spacer">&nbsp;</div>';
191
        let locDesc = '';
192
193
        if (selFeature.get('rowId') !== undefined) {
194
            pContent += '<span class="rowId">' + selFeature.get('rowId') + ': </span>';
195
        }
196
        if (selFeature.get('name') !== undefined) {
197
            pContent += '<span class="txt">' + selFeature.get('name') + '</span>';
198
            locDesc = selFeature.get('name');
0 ignored issues
show
Unused Code introduced by
The variable locDesc seems to be never used. Consider removing it.
Loading history...
199
            // TODO strip <p> tag from locDesc
200
            // locDesc = selFeature.get('name').split(/\s+/).slice(0,2).join('+');
201
        }
202
        if (selFeature.get('ele') !== undefined) {
203
            pContent += '<div class="ele">elevation: ' + selFeature.get('ele') + '</div>';
204
        }
205
        if (selFeature.get('type') !== undefined) {
206
            pContent += '<div>' + selFeature.get('type') + '</div>';
207
        }
208
        if (selFeature.get('time') !== undefined) {
209
            pContent += '<div class="time">time: ' + selFeature.get('time') + '</div>';
210
        }
211
        if (selFeature.get('description') !== undefined) {
212
            pContent += '<div class="desc">' + selFeature.get('description') + '</div>';
213
        }
214
        if (selFeature.get('img') !== undefined) {
215
            pContent += '<div class="coord" title="lat;lon">' + '<img src="' + selFeature.get('img') + '" width="16" height="16" ' + 'style="transform:rotate(' + selFeature.get('angle') + 'deg)" />&nbsp;' + '<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>';
0 ignored issues
show
Bug introduced by
The variable ol seems to be never declared. If this is a global, consider adding a /** global: ol */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
216
        }
217
        content.innerHTML = pContent;
218
    } else {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
219
        // do nothing?...
220
    }
221
});
222
223
// change mouse cursor when over marker
224
map.on('pointermove', function (e) {
225
    const pixel = map.getEventPixel(e.originalEvent);
226
    const hit = map.hasFeatureAtPixel(pixel);
227
    map.getTarget().style.cursor = hit ? 'pointer' : '';
228
});
229