-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathPage through POI results.html
314 lines (248 loc) · 12.1 KB
/
Page through POI results.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page through POI results - 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 step through all the results available for a POI query. This sample also creates a list of the results and cross references the list items to the shapes on the map." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, page results, paging results, pagination" />
<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 a reference to the Azure Maps Services Module JavaScript file. -->
<script src="https://atlas.microsoft.com/sdk/javascript/service/2/atlas-service.min.js"></script>
<script>
var map, datasource, searchURL, totalResults, query;
//The current page index.
var pageIdx = 0;
//The number of results to retrieve per "page".
var pageSize = 10;
//Initial query options for the POI search, biased towards the initial center of the map.
var queryOptions = {
radius: 5000,
ofs: pageIdx,
limit: pageSize,
view: 'Auto'
};
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-122.33, 47.6],
zoom: 12,
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Microsoft Entra ID authentication.
authType: 'anonymous',
clientId: 'e6b6ab59-eb5d-4d25-aa57-581135b927f0', //Your Azure Maps client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Microsoft Entra ID Token.
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsToken';
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]'
}
});
//Use MapControlCredential to share authentication between a map control and the service module.
var pipeline = atlas.service.MapsURL.newPipeline(new atlas.service.MapControlCredential(map));
//Create an instance of the SearchURL client.
searchURL = new atlas.service.SearchURL(pipeline);
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a reusable popup.
popup = new atlas.Popup();
//Create a data source to add your data to.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Add a bubble layer to render the points of interests.
var poiLayer = new atlas.layer.BubbleLayer(datasource, null, {
createIndicators: true, // to enable bubble layer a11y feature
color: '#007343'
});
map.layers.add(poiLayer);
//Add a click event to the poi layer.
map.events.add('click', poiLayer, (e) => {
showPopup(e.shapes[0]);
});
//Add a mouse move move event to the poi layer and have it highlight the releated item in the list.
map.events.add('mousemove', poiLayer, highlightListItem);
//When the mouse leaves the item on the layer, change the cursor back to the default which is grab.
map.events.add('mouseout', poiLayer, () => {
map.getCanvasContainer().style.cursor = 'grab';
});
//Make an initial query.
updatePoiQuery();
});
}
function updatePoiQuery() {
//Get the users query.
query = document.getElementById('queryTbx').value;
//Reset the page index and total results.
pageIdx = 0;
totalResults = 0;
var center = map.getCamera().center;
//Update the location to bias the results.
queryOptions.lon = center[0];
queryOptions.lat = center[1];
getNearbyPoi();
}
function getNearbyPoi() {
//Close popup.
popup.close();
updatePageBtns();
//Update the query options to skip results based on the page index and size.
queryOptions.ofs = pageIdx * pageSize,
//Make a query for nearby points of interest.
searchURL.searchPOI(atlas.service.Aborter.timeout(3000), query, queryOptions).then(results => {
//If the initial search has just been performed, capture the total number of results available for the query.
if (results.summary.offset === 0) {
totalResults = results.summary.totalResults;
updatePageBtns();
}
//Calculate the start and end result index.
var start = pageIdx * pageSize + 1;
var end = start + results.summary.numResults - 1;
//Display details on the number of results and result index.
document.getElementById('pageInfo').innerText = `Results: ${start} - ${end} of ${totalResults} results`;
//Get GeoJSON versions of the results.
var data = results.geojson.getFeatures();
//Loop through and create a list of the results.
var listHTML = ['<table>'];
//Create HTML for each line item in the list.
for (var i = 0, len = data.features.length; i < len; i++) {
var f = data.features[i];
listHTML.push(
//Add a column of index numbers. Pass in the ID of each result into the rel attribute for cross referencing the list item to the shape.
`<tr rel="${f.id}" onmouseover="listItemClicked('${f.id}');" onmouseout="popup.close();" style="cursor:pointer"><td valign="top"> ${start + i}) </td>`,
//Add some information about the list item.
`<td><b>${f.properties.poi.name}</b><br/>${f.properties.address.freeformAddress}</td>`,
//Create a column to display the distance to the location.
`<td>${atlas.math.convertDistance(f.properties.dist, 'meters', 'miles', 2)} mi(s)</td></tr>`);
//Add the result index as a property of the feature.
f.properties._listIdx = start + i + '';
}
listHTML.push('</table>');
document.getElementById('resultList').innerHTML = listHTML.join('');
//Add the GeoJSON data to the map.
datasource.setShapes(data);
//Have the map camera fit the data.
map.setCamera({
bounds: atlas.data.BoundingBox.fromData(data),
padding: 50
});
});
}
function showPopup(shape) {
var prop = shape.getProperties();
//Update the content and position of the popup.
popup.setOptions({
//Create a table from the properties of the shape.
content: `<div style="padding:10px;"><b>${prop.poi.name}</b><br/>${prop.address.freeformAddress}</div>`,
position: shape.getCoordinates()
});
//Open the popup.
popup.open(map);
}
function listItemClicked(id) {
//Retrieve the shape for the list item from the data source.
var shape = datasource.getShapeById(id);
//Show the popup for the shape.
showPopup(shape);
}
function pageBackwards() {
if (pageIdx > 0) {
pageIdx--;
getNearbyPoi();
}
}
function pageForward() {
//Ensure that paging does not exceed the number of results.
if ((pageIdx + 1) * pageSize < totalResults) {
pageIdx++;
getNearbyPoi();
}
}
function updatePageBtns() {
//Update the disabled state of the page buttons based on the page index and number of results.
document.getElementById('pageBackBtn').disabled = (pageIdx === 0);
document.getElementById('pageFwdBtn').disabled = ((pageIdx + 1) * pageSize >= totalResults);
}
function highlightListItem(e) {
//Retrieve the list item based on the id of a shape.
var elm = getListItemById(e.shapes[0].getId());
if (elm) {
//Highlight the list item to indicate that its shape has been hovered.
elm.style.backgroundColor = 'LightGreen';
//Remove the highlighting after a second.
setTimeout(function () {
elm.style.backgroundColor = 'white';
}, 1000);
}
//Change the mouse pointer over the shape.
map.getCanvasContainer().style.cursor = 'pointer';
}
function getListItemById(id) {
var listItems = document.getElementById('resultList').getElementsByTagName('tr');
for (var i = 0, len = listItems.length; i < len; i++) {
var rel = listItems[i].getAttribute('rel');
if (rel === id) {
return listItems[i];
}
}
return null;
}
</script>
<style>
.mapContainer {
position: relative;
width: 100%;
min-width: 290px;
height: 600px;
}
.sidePanel {
position: relative;
float: left;
margin-left: 10px;
width: 300px;
height: 600px;
}
#resultList{
height: 485px;
overflow-y: auto;
}
#myMap {
position: relative;
float: left;
width: calc(100% - 310px);
height: 600px;
}
</style>
</head>
<body onload="getMap()">
<div class="mapContainer">
<div class="sidePanel">
<table style="margin-bottom:10px;">
<tr>
<td><input id="queryTbx" type="text" value="Starbucks" /></td>
<td><input type="button" onclick="updatePoiQuery()" value="Search" /></td>
</tr>
</table>
<input id="pageBackBtn" type="button" value="<" onclick="pageBackwards();" />
<input id="pageFwdBtn" type="button" value=">" onclick="pageForward();" /><br /><br />
<div id="pageInfo"></div><br />
<div id="resultList"></div>
</div>
<div id="myMap"></div>
</div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Page through POI results</legend>
This sample shows how to step through all the results available for a POI query.
This sample also creates a list of the results and cross references the list items to the shapes on the map.
</fieldset>
</body>
</html>