Skip to content

Commit fbf26db

Browse files
authored
update js
1 parent 8808a28 commit fbf26db

1 file changed

Lines changed: 63 additions & 6 deletions

File tree

  • Samples/Tutorials/Simple Store Locator

‎Samples/Tutorials/Simple Store Locator/index.js‎

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,19 @@ function initialize() {
2828
// Use SAS token for authentication
2929
authType: 'sas',
3030
getToken: function (resolve, reject, map) {
31+
// URL to your authentication service that retrieves a SAS Token
3132
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
33+
3234
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
3335
}
36+
37+
//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.
38+
//authType: 'subscriptionKey',
39+
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
3440
}
41+
//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.
42+
//authType: 'subscriptionKey',
43+
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
3544
});
3645

3746
//Create a popup but leave it closed so we can update it and display it later.
@@ -273,12 +282,11 @@ async function geocodeSelectedLocation(selectedEntity, autocompleteItem) {
273282
var result = json.features[0];
274283
var coordinates = result.geometry.coordinates;
275284

276-
var radius = 0.1;
277285
var bbox = [
278-
coordinates[0] - radius,
279-
coordinates[1] - radius,
280-
coordinates[0] + radius,
281-
coordinates[1] + radius
286+
coordinates[0] - 0.1,
287+
coordinates[1] - 0.1,
288+
coordinates[0] + 0.1,
289+
coordinates[1] + 0.1
282290
];
283291

284292
map.setCamera({
@@ -334,12 +342,15 @@ function performSearch() {
334342
}
335343

336344
function setMapToUserLocation() {
345+
//Request the user's location.
337346
navigator.geolocation.getCurrentPosition(function (position) {
347+
//Convert the geolocation API position into a longitude/latitude position value the map can understand and center the map over it.
338348
map.setCamera({
339349
center: [position.coords.longitude, position.coords.latitude],
340350
zoom: maxClusterZoomLevel + 1
341351
});
342352
}, function (error) {
353+
//If an error occurs when trying to access the users position information, display an error message.
343354
switch (error.code) {
344355
case error.PERMISSION_DENIED:
345356
alert('User denied the request for Geolocation.');
@@ -375,19 +386,36 @@ function updateListItems() {
375386

376387
listPanel.innerHTML = '<div class="statusMessage">Search for a location, zoom the map, or press the "My Location" button to see individual locations.</div>';
377388
} else {
389+
//Update the location of the centerMarker.
378390
centerMarker.setOptions({
379391
position: camera.center,
380392
visible: true
381393
});
382394

395+
//List the ten closest locations in the side panel.
383396
var html = [], properties;
384397

398+
/*
399+
Generating HTML for each item that looks like this:
400+
401+
<div class="listItem" onclick="itemSelected('id')">
402+
<div class="listItem-title">1 Microsoft Way</div>
403+
Redmond, WA 98052<br />
404+
Open until 9:00 PM<br />
405+
0.7 miles away
406+
</div>
407+
*/
408+
409+
//Get all the shapes that have been rendered in the bubble layer.
385410
var data = map.layers.getRenderedShapes(map.getCamera().bounds, [iconLayer]);
386411

412+
//Create an index of the distances of each shape.
387413
var distances = {};
388414

389415
data.forEach(function (shape) {
390416
if (shape instanceof atlas.Shape) {
417+
418+
//Calculate the distance from the center of the map to each shape and store in the index. Round to 2 decimals.
391419
distances[shape.getId()] = Math.round(atlas.math.getDistanceTo(camera.center, shape.getCoordinates(), 'miles') * 100) / 100;
392420
}
393421
});
@@ -459,14 +487,19 @@ function getOpenTillTime(properties) {
459487
return 'Open until ' + sTime;
460488
}
461489

490+
//When a user clicks on a result in the side panel, look up the shape by its id value and show popup.
462491
function itemSelected(id) {
492+
//Get the shape from the data source using it's id.
463493
var shape = datasource.getShapeById(id);
464494
showPopup(shape);
465495

496+
//Center the map over the shape on the map.
466497
var center = shape.getCoordinates();
467498
var offset;
468499

500+
//If the map is less than 700 pixels wide, then the layout is set for small screens.
469501
if (map.getCanvas().width < 700) {
502+
//When the map is small, offset the center of the map relative to the shape so that there is room for the popup to appear.
470503
offset = [0, -80];
471504
}
472505

@@ -479,6 +512,26 @@ function itemSelected(id) {
479512
function showPopup(shape) {
480513
var properties = shape.getProperties();
481514

515+
/*
516+
Generating HTML for the popup that looks like this:
517+
518+
<div class="storePopup">
519+
<div class="popupTitle">
520+
3159 Tongass Avenue
521+
<div class="popupSubTitle">Ketchikan, AK 99901</div>
522+
</div>
523+
<div class="popupContent">
524+
Open until 22:00 PM<br/>
525+
<img title="Phone Icon" src="images/PhoneIcon.png">
526+
<a href="tel:1-800-XXX-XXXX">1-800-XXX-XXXX</a>
527+
<br>Amenities:
528+
<img title="Wi-Fi Hotspot" src="images/WiFiIcon.png">
529+
<img title="Wheelchair Accessible" src="images/WheelChair-small.png">
530+
</div>
531+
</div>
532+
*/
533+
534+
//Calculate the distance from the center of the map to the shape in miles, round to 2 decimals.
482535
var distance = Math.round(atlas.math.getDistanceTo(map.getCamera().center, shape.getCoordinates(), 'miles') * 100)/100;
483536

484537
var html = ['<div class="storePopup">'];
@@ -516,14 +569,18 @@ function showPopup(shape) {
516569

517570
html.push('</div></div>');
518571

572+
//Update the content and position of the popup for the specified shape information.
519573
popup.setOptions({
574+
//Create a table from the properties in the feature.
520575
content: html.join(''),
521576
position: shape.getCoordinates()
522577
});
523578

579+
//Open the popup.
524580
popup.open(map);
525581
}
526582

583+
//Creates an addressLine2 string consisting of City, Municipality, AdminDivision, and PostCode.
527584
function getAddressLine2(properties) {
528585
var html = [properties['City']];
529586

@@ -543,4 +600,4 @@ function getAddressLine2(properties) {
543600
}
544601

545602
//Initialize the application when the page is loaded.
546-
window.onload = initialize;
603+
window.onload = initialize;

0 commit comments

Comments
 (0)