-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
187 lines (161 loc) · 5.21 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="teritorio.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="og:description"
content="Allow visualization and interaction with all markers, even when superposed. Can display and interact with small cluster without the need to zoom or uncluster." />
<title>MapLibre GL Teritorio Cluster</title>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css">
<script type="module" src="https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script>
<style>
body {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
html,
body,
#map {
height: 100%;
}
header {
display: flex;
padding: 8px;
width: 100%;
}
</style>
</head>
<body>
<header>
<input id="custom-url" type="url" placeholder="GeoJSON source URL..." />
<button id="load-data">Load external data</button>
|
<input id="feature-id" type="text" value="us2000ahxc" />
<button id="select-feature">Select feature manually</button>
</header>
<div id="map"></div>
<script type="module">
import { buildCss, unfoldedClusterRenderSmart as unfoldedClusterRender, TeritorioCluster } from './src/index'
let geojson;
let teritorioCluster;
const map = new maplibregl.Map({
hash: true,
container: "map",
style: 'https://api.maptiler.com/maps/openstreetmap/style.json?key=GIAfARJO9HDgKceZRlGv'
});
map.addControl(new maplibregl.NavigationControl());
map.on('load', async () => {
geojson = await fetch('https://maplibre.org/maplibre-gl-js/docs/assets/earthquakes.geojson', { method: 'GET' }).then(res => res.json())
// add a clustered GeoJSON source for a sample set of earthquakes
map.addSource('earthquakes', {
'type': 'geojson',
'data': geojson,
'cluster': true,
'clusterRadius': 80,
'clusterMaxZoom': 22,
'maxzoom': 24
})
map.addLayer({
'id': 'earthquake_label',
'type': 'symbol',
'source': 'earthquakes',
'filter': ['!=', 'cluster', true],
'layout': {
'text-field': [
'number-format',
['get', 'mag'],
{ 'min-fraction-digits': 1, 'max-fraction-digits': 1 }
],
'text-font': ['Open Sans Semibold'],
'text-size': 10
},
'paint': {
'text-color': 'white'
}
});
teritorioCluster = new TeritorioCluster(
map,
'earthquakes',
{
clusterRenderFn: clusterRender,
fitBoundsOptions: { padding: { top: 57, bottom: 20, left: 20, right: 20 } },
markerRenderFn: markerRender,
markerSize: 28,
unfoldedClusterRenderFn: unfoldedClusterRender,
pinMarkerRenderFn: pinMarkerRender
},
)
// Get feature click event
teritorioCluster.addEventListener('feature-click', (e) => {
console.log(e.detail.selectedFeature)
})
document.getElementById('load-data').addEventListener('click', loadData)
document.getElementById('select-feature').addEventListener('click', selectFeature)
})
const clusterRender = (element, props) => {
element.innerHTML = props.point_count.toLocaleString();
buildCss(element, {
'background-color': 'white',
'border-radius': '100%',
'border': '2px solid green',
'justify-content': 'center',
'align-items': 'center',
'display': 'flex',
'color': 'black',
'width': `60px`,
'height': `60px`,
'cursor': 'pointer'
});
}
const loadData = async () => {
const url = document.getElementById('custom-url').value
if (!url)
return
let vidoGeojson = await fetch(url, { method: 'GET' }).then(res => res.json())
const source = map.getSource('earthquakes')
source.setData({
type: 'FeatureCollection',
features: [...geojson.features, ...vidoGeojson.features]
})
}
const markerRender = (element, feature, markerSize) => {
element.textContent = feature.properties?.mag
buildCss(element, {
'background-color': 'green',
'border-radius': '100%',
'justify-content': 'center',
'align-items': 'center',
'display': 'flex',
'color': 'white',
'width': `${markerSize}px`,
'height': `${markerSize}px`,
'cursor': 'pointer'
});
}
const pinMarkerRender = (coords, offset) => {
return new maplibregl.Marker({
scale: 1.3,
color: '#f44336',
anchor: 'bottom'
})
.setLngLat(coords)
.setOffset(offset)
}
const selectFeature = async () => {
const id = document.getElementById('feature-id').value
if (!id)
return
const data = await map.getSource('earthquakes').getData()
const feature = data.features.find(feature => feature.properties.id === id)
if (!feature) {
console.error('Feature not found !')
return
}
teritorioCluster.setSelectedFeature(feature)
}
</script>
</body>
</html>