-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathPlaces.html
More file actions
254 lines (222 loc) · 9.55 KB
/
Places.html
File metadata and controls
254 lines (222 loc) · 9.55 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
252
253
254
<!DOCTYPE html>
<html lang="en">
<head>
<title>PMTiles Places - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to leverage PMTiles as custom protocol to create data layers using Overture Places dataset." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, pmtiles, pmtile, Overture, place" />
<meta name="author" content="Microsoft Azure Maps" />
<meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.jpg" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
<!-- Add references to the PMTiles lib. -->
<script src="https://unpkg.com/pmtiles@3.0.5/dist/pmtiles.js"></script>
<style>
#map {
width: 100%;
height: 600px;
}
#panel {
top: 15px;
left: 15px;
position: absolute;
background-color: white;
border-radius: 10px;
padding: 10px;
}
#info {
margin-top: 15px;
}
.atlas-map .azure-maps-control-popup-template-content {
overflow-x: hidden;
}
.atlas-map .azure-maps-control-popup-template {
padding: 20px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="panel">
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'coffee_shop')" id="coffee" ><label for="coffee">Coffee</label><br />
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'gym')" id="gym" ><label for="gym">Gym</label><br />
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'library')" id="library" ><label for="library">Library</label><br />
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'museum')" id="museum" ><label for="museum">Museum</label><br />
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'park')" id="park" ><label for="park">Park</label><br />
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'parking')" id="parking" ><label for="parking">Parking</label><br />
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'shopping')" id="shopping" ><label for="shopping">Shopping</label><br />
</div>
<fieldset id="info">
<legend>PMTiles - Places</legend>
This sample showcases the use of <a href="https://protomaps.com/" target="_blank">Protomaps</a> to display <a href="https://overturemaps.org/" target="_blank">Overture</a> places data on a map.
The data is stored in a PMTiles file that contains a vector tileset of places data. The data is filtered by category and displayed as symbols on the map.
Click on a symbol to view more information about the place.
</fieldset>
<script>
var map, layer, popup;
var categories = ['coffee_shop', 'gym', 'library', 'museum', 'park', 'parking', 'shopping'];
//Initialize the plugin.
const protocol = new pmtiles.Protocol();
atlas.addProtocol("pmtiles", (request) => {
return new Promise((resolve, reject) => {
const callback = (err, data) => {
if (err) {
reject(err);
} else {
resolve({ data });
}
};
protocol.tile(request, callback);
});
});
const PMTILES_URL = "https://overturemaps-tiles-us-west-2-beta.s3.amazonaws.com/2024-07-22/places.pmtiles";
protocol.add(new pmtiles.PMTiles(PMTILES_URL));
//Initialize the map.
map = new atlas.Map("map", {
//Add authentication details for connecting to Azure Maps.
authOptions: {
// Use SAS token for authentication
authType: 'sas',
getToken: function (resolve, reject, map) {
// URL to your authentication service that retrieves a SAS Token
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
},
center: [-73.96983, 40.77926],
zoom: 15,
});
//Wait until the map resources are ready.
map.events.add("ready", function () {
//Add the source to the map.
map.sources.add(
new atlas.source.VectorTileSource("pmtiles", {
type: "vector",
url: `pmtiles://${PMTILES_URL}`,
})
);
//Create a symbol layer.
layer = new atlas.layer.SymbolLayer(
"pmtiles",
"places",
{
sourceLayer: "place",
iconOptions: {
image: [
"case",
[">=", ["index-of", `\"primary\":\"coffee_shop\"`, ["get", "categories"]], 0],
"coffee",
[">=", ["index-of", `\"primary\":\"gym\"`, ["get", "categories"]], 0],
"gym",
[">=", ["index-of", `\"primary\":\"library\"`, ["get", "categories"]], 0],
"library",
[">=", ["index-of", `\"primary\":\"museum\"`, ["get", "categories"]], 0],
"museum",
[">=", ["index-of", `\"primary\":\"parking\"`, ["get", "categories"]], 0],
"parking",
[">=", ["index-of", `\"primary\":\"park\"`, ["get", "categories"]], 0],
"park",
[">=", ["index-of", `\"primary\":\"shopping\"`, ["get", "categories"]], 0],
"shopping",
"none",
],
allowOverlap: true,
ignorePlacement: true,
},
filter: [
"all",
["has", "@name"],
["has", "categories"],
[
"any",
...categories.map((category) => [
">=",
["index-of", `\"primary\":\"${category}\"`, ["get", "categories"]],
0,
]),
]
],
}
);
//Add a click event to the layer.
map.events.add("click", layer, featureClicked);
//Add the layer to the map.
map.layers.add([layer]);
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup({ position: [0, 0] });
//Add map controls.
map.controls.add([
new atlas.control.StyleControl({
mapStyles: ['road', 'satellite', 'satellite_road_labels', 'night', 'grayscale_light', 'grayscale_dark', 'road_shaded_relief', 'high_contrast_light', 'high_contrast_dark']
})
], {
position: 'top-right'
});
});
function featureClicked(e) {
//Make sure the event occurred on a shape feature.
if (e.shapes && e.shapes.length > 0) {
//By default, show the popup where the mouse event occurred.
var pos = e.position;
var offset = [0, 0];
var properties = e.shapes[0].properties;
//Parse the JSON properties.
properties.addresses = properties.addresses ? JSON.parse(properties.addresses) : undefined;
properties.categories = properties.categories ? JSON.parse(properties.categories) : undefined;
properties.names = properties.names ? JSON.parse(properties.names) : undefined;
properties.phones = properties.phones ? JSON.parse(properties.phones) : undefined;
properties.socials = properties.socials ? JSON.parse(properties.socials) : undefined;
properties.sources = properties.sources ? JSON.parse(properties.sources) : undefined;
properties.websites = properties.websites ? JSON.parse(properties.websites) : undefined;
//Update the content and position of the popup.
popup.setOptions({
//Create a table from the properties in the feature.
content: atlas.PopupTemplate.applyTemplate(properties, { sandboxContent: false }),
position: pos,
pixelOffset: offset
});
//Open the popup.
popup.open(map);
}
}
function filterSymbols(elm, category) {
if (elm.checked) {
//Add the category to the categories array.
categories.push(category);
} else {
//Remove the category from the categories array.
categories.splice(categories.indexOf(category), 1);
}
//Create a filter for the categories.
var filter = [
"all",
["has", "@name"],
["has", "categories"],
[
"any",
...categories.map((category) => [
">=",
["index-of", `\"primary\":\"${category}\"`, ["get", "categories"]],
0,
]),
]
]
//Update the filter for the layer.
layer.setOptions({
filter: filter
});
//Close the popup.
if (popup) {
popup.close();
}
}
</script>
</body>
</html>