-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
166 lines (129 loc) · 4.21 KB
/
main.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
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
fetch('/data/gem31122019flaechen.geojson', {
method: 'GET'
})
.then((response) => {
return response.json()
})
.then((data) => {
addData(data);
})
.catch(function (error) {
console.log(error);
})
const map = L.map('map', {
maxZoom: 13
}).setView([54.7836, 9.4321], 13);
let prevLayerClicked = null;
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map)
let geocoder = L.Control.Geocoder.nominatim()
if (typeof URLSearchParams !== 'undefined' && location.search) {
// parse /?geocoder=nominatim from URL
let params = new URLSearchParams(location.search)
let geocoderString = params.get('geocoder')
if (geocoderString && L.Control.Geocoder[geocoderString]) {
console.log('Using geocoder', geocoderString)
geocoder = L.Control.Geocoder[geocoderString]()
} else if (geocoderString) {
console.warn('Unsupported geocoder', geocoderString)
}
}
const osmGeocoder = new L.Control.geocoder({
query: 'Flensburg',
position: 'topright',
placeholder: 'Adresse oder Ort',
defaultMarkGeocode: false
}).addTo(map)
osmGeocoder.on('markgeocode', e => {
const bounds = L.latLngBounds(e.geocode.bbox._southWest, e.geocode.bbox._northEast)
map.fitBounds(bounds, {padding: [200, 200]})
})
let layerStyle = {
default: {
color: '#fff',
fillColor: '#002db4',
fillOpacity: 0.7,
opacity: 0.6,
weight: 1
},
click: {
color: '#fff',
fillColor: '#002db4',
fillOpacity: 0.4,
opacity: 0.8,
weight: 4
}
}
function onMapClick(e) {
const bounds = L.latLngBounds(e.target._bounds._southWest, e.target._bounds._northEast)
map.fitBounds(bounds, {padding: [200, 200]})
const dataArray = []
const infoArray = []
const unitArray = []
infoArray.push('Waldfläche')
infoArray.push('Landwirtschaftsfläche')
infoArray.push('Verkehrsfläche')
infoArray.push('Siedlungs- und Verkehrsfläche¹')
infoArray.push('Siedlungs- und Verkehrsfläche¹')
dataArray.push(e.target.feature.properties.a)
dataArray.push(e.target.feature.properties.b)
dataArray.push(e.target.feature.properties.c)
dataArray.push(e.target.feature.properties.d)
dataArray.push(e.target.feature.properties.e)
unitArray.push('%')
unitArray.push('%')
unitArray.push('%')
unitArray.push('%')
unitArray.push('m²/Kopf')
const h2 = document.createElement('h2')
h2.innerHTML = e.target.feature.properties.f
h2.classList.add('text-2xl')
h2.classList.add('font-bold')
h2.classList.add('px-3')
h2.classList.add('py-2')
const list = document.createElement('ul')
const small = document.createElement('small')
small.classList.add('p-3')
small.innerHTML = '¹ Ohne Bergbaubetrieb sowie Tagebau, Grube, Steinbruch'
small.classList.add('inline-block')
list.classList.add('p-3')
for (let i = 0; i < dataArray.length; i++) {
const item = document.createElement('li')
const entry = `<p>${infoArray[i]}</p><strong>${dataArray[i]} ${unitArray[i]}</strong>`
item.classList.add('mb-2')
item.innerHTML = entry
list.appendChild(item)
}
const details = document.getElementById('details')
details.innerHTML = ''
details.appendChild(h2)
details.appendChild(list)
details.appendChild(small)
details.classList.add('mb-4')
e.preventDefault
}
function onEachFeature(feature, layer) {
const label = feature.properties.f
layer.on('click', function(e) {
e.target.setStyle(layerStyle.click);
if (prevLayerClicked !== null) {
prevLayerClicked.setStyle(layerStyle.default);
}
const layer = e.target;
prevLayerClicked = layer;
onMapClick(e)
})
layer.bindTooltip(label, {
permanent: false,
direction: 'top'
}).openTooltip()
}
function addData(data) {
const layer = L.geoJson(data, {
style: layerStyle.default,
onEachFeature: onEachFeature
}).addTo(map)
map.fitBounds(layer.getBounds(), {padding: [0, 0, 0, 0]})
}