This repository was archived by the owner on Dec 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhajk-light.js
113 lines (97 loc) · 2.75 KB
/
hajk-light.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
async function Hajk(settings) {
// Define some global config objects
let config = {},
layersConfig,
mapConfig;
/**
* Parses config given at init. Takes care of decoding query params
* as well. Places it all to the global 'c' object.
*/
function parseConfig(settings) {
const { urlToMapservice, injectToDOMelementWithId } = settings;
const searchParams = new URLSearchParams(settings.queryParams);
const m = searchParams.get("m");
const x = searchParams.get("x");
const y = searchParams.get("y");
const z = searchParams.get("z");
const l = searchParams.get("l");
config = {
injectToDOMelementWithId,
urlToMapservice,
m,
x,
y,
z,
l
};
}
/**
* Fetches both map config and layers config.
* Places them in two global config files.
*/
async function fetchAll() {
let urls = [
`${config.urlToMapservice}config/layers`,
`${config.urlToMapservice}config/${config.m}`
];
await Promise.all(
urls.map(url => fetch(url).then(resp => resp.json()))
).then(data => {
layersConfig = data[0];
mapConfig = data[1];
});
}
/**
* Loops through layer IDs specified in the "l" parameter. Creates a
* valid OL Layer object for each layer. Returns an array, ready to be
* supplied to ol.Map() later on.
*/
function getLayers() {
let allLayers = [];
config.l.split(",").forEach(id => {
// TODO: Similar for all other layer types
let layer = layersConfig.wmslayers.find(match => match.id === id);
console.log(layer);
allLayers.push(
new ol.layer.Tile({
extent: mapConfig.map.extent,
source: new ol.source.TileWMS({
url: layer.url,
params: { LAYERS: layer.layers.join(), TILED: layer.tiled },
serverType: "geoserver",
transition: 0
})
})
);
});
return allLayers;
}
function injectMap() {
let mapEl = config.injectToDOMelementWithId || "map";
const mapProjection = mapConfig.projections.find(
p => p.code === mapConfig.map.projection
);
proj4.defs(mapProjection.code, mapProjection.definition);
ol.proj.proj4.register(proj4);
var map = new ol.Map({
target: mapEl,
layers: getLayers(),
view: new ol.View({
center: ol.proj.fromLonLat([config.x / 10000, config.y / 100000]),
zoom: config.z,
projection: ol.proj.get(mapProjection.code)
})
});
}
/**
* Main thread starts here.
*/
parseConfig(settings);
await fetchAll();
console.log("config: ", config);
console.log("mapConfig: ", mapConfig);
console.log("layersConfig: ", layersConfig);
console.log("ol: ", ol);
injectMap();
}
document.Hajk = Hajk;