forked from robertleeplummerjr/Leaflet.glify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
251 lines (232 loc) · 6.91 KB
/
example.ts
File metadata and controls
251 lines (232 loc) · 6.91 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import * as L from "leaflet";
import { LeafletMouseEvent } from "leaflet";
import { Feature, FeatureCollection, LineString, MultiPolygon } from "geojson";
import glify from "./src/index";
const map = L.map("map").setView([50.0, 14.44], 7);
L.tileLayer(
"https://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}{r}.png"
).addTo(map);
Promise.all([
wget<number[][]>("data/86T.json"),
wget<FeatureCollection>("data/CZDistricts.json"),
wget<FeatureCollection<LineString>>("data/rivers.json"),
wget<Feature<MultiPolygon>>("data/antarctica.geojson"),
]).then(([points, districts, rivers, antarctica]) => {
glify.shapes({
map: map,
click: (e, feature): void => {
L.popup()
.setLatLng(e.latlng)
.setContent(`You clicked on ${feature.properties.NAZKR_ENG}`)
.openOn(map);
console.log("clicked on Shape", feature, e);
},
contextMenu: (e, feature): void => {
e.originalEvent.preventDefault(); // Prevent the default context menu from showing
L.popup()
.setLatLng(e.latlng)
.setContent(`You right clicked on ${feature.properties.NAZKR_ENG}`)
.openOn(map);
console.log("clicked on Shape", feature, e);
},
hover: (e: LeafletMouseEvent, feature) => {
console.log("hovered on Shape", feature, e);
},
data: districts,
border: true,
});
glify.lines({
map,
latitudeKey: 1,
longitudeKey: 0,
weight: 2,
click: (e: LeafletMouseEvent, feature) => {
L.popup()
.setLatLng(e.latlng)
.setContent(`clicked on Line ${feature.properties.name}`)
.openOn(map);
console.log("clicked on Line", feature, e);
},
contextMenu: (e: LeafletMouseEvent, feature) => {
e.originalEvent.preventDefault(); // Prevent the default context menu from showing
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(e.latlng)
.setContent(`right clicked on Line ${feature.properties.name}`)
.openOn(map);
},
hover: (e: LeafletMouseEvent, feature) => {
console.log("hovered on Line", feature, e);
},
hoverOff: (e: LeafletMouseEvent, feature) => {
console.log("hovered off Line", feature, e);
},
data: rivers,
});
glify.points({
map: map,
size: function (i) {
return Math.random() * 17 + 3;
},
hover: (e: LeafletMouseEvent, feature) => {
console.log("hovered on Point", feature, e);
},
click: (e: LeafletMouseEvent, feature) => {
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(feature)
.setContent(
`You clicked the point at longitude:${e.latlng.lng}, latitude:${e.latlng.lat}`
)
.openOn(map);
console.log("clicked on Point", feature, e);
},
contextMenu: (e: LeafletMouseEvent, feature) => {
e.originalEvent.preventDefault(); // Prevent the default context menu from showing
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(feature)
.setContent(
`You right clicked the point at longitude:${e.latlng.lng}, latitude:${e.latlng.lat}`
)
.openOn(map);
},
data: points,
});
glify.points({
map,
size: (i) => {
return 20;
},
color: () => {
return {
r: 1,
g: 0,
b: 0,
};
},
click: (e: LeafletMouseEvent, feature) => {
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(feature)
.setContent(
`You clicked the point at longitude:${e.latlng.lng}, latitude:${e.latlng.lat}`
)
.openOn(map);
console.log("clicked on Point", feature, e);
},
contextMenu: (e: LeafletMouseEvent, feature) => {
e.originalEvent.preventDefault(); // Prevent the default context menu from showing
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(feature)
.setContent(
`You right clicked the point at longitude:${e.latlng.lng}, latitude:${e.latlng.lat}`
)
.openOn(map);
console.log("clicked on Point", feature, e);
},
hover: (e: LeafletMouseEvent, feature) => {
console.log("hovered on Point", feature, e);
},
data: [[50.10164799, 14.5]],
});
glify.points({
map,
size: (i) => {
return 20;
},
color: () => {
return {
r: 0,
g: 0,
b: 1,
};
},
hover: (e: LeafletMouseEvent, feature) => {
console.log("hovered on Point", feature, e);
},
hoverOff: (e: LeafletMouseEvent, feature) => {},
click: (e, feature) => {
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(feature.geometry.coordinates)
.setContent("You clicked on:" + feature.properties.name)
.openOn(map);
console.log("clicked on Point", feature, e);
},
contextMenu: (e, feature) => {
e.originalEvent.preventDefault(); // Prevent the default context menu from showing
//set up a standalone popup (use a popup as a layer)
L.popup()
.setLatLng(feature.geometry.coordinates)
.setContent("You right clicked on:" + feature.properties.name)
.openOn(map);
console.log("clicked on Point", feature, e);
},
data: {
//geojson
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [90, 135],
},
properties: {
name: "North Pole",
color: "red",
},
},
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [90, 45],
},
properties: {
name: "South Pole",
color: "blue",
},
},
],
},
});
glify.shapes({
map,
data: antarctica,
border: true,
click: (e, feature) => {
L.popup()
.setLatLng(e.latlng)
.setContent(`You clicked on ${feature.properties.name}`)
.openOn(map);
console.log("clicked on Shape", feature, e);
},
contextMenu: (e, feature) => {
e.originalEvent.preventDefault(); // Prevent the default context menu from showing
L.popup()
.setLatLng(e.latlng)
.setContent(`You right clicked on ${feature.properties.name}`)
.openOn(map);
console.log("clicked on Shape", feature, e);
},
hover: (e, feature) => {
console.log("hovered on Shape", feature, e);
},
});
});
function wget<T>(url: string): Promise<T> {
return new Promise<T>((resolve, reject) => {
const request = new XMLHttpRequest();
request.open("GET", url, true);
request.onload = () => {
if (request.status < 200 && request.status > 400) {
return reject(new Error("failure"));
}
resolve(JSON.parse(request.responseText) as T);
};
request.send();
});
}