Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
AzureMapsGovCloudCodeSamples/AzureMapsCodeSamples/Services Module/Page through POI results.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
315 lines (249 sloc)
12.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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 http-equiv="x-ua-compatible" content="IE=Edge" /> | |
<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" /> | |
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. --> | |
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" /> | |
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/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 type='text/javascript'> | |
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() { | |
//Point the Azure Maps domain to the US Azure Gov Cloud domain. | |
atlas.setDomain('atlas.azure.us'); | |
//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 Azure Active Directory authentication. | |
authType: 'anonymous', | |
clientId: 'c9f2f391-13f1-407b-a4a5-f0a241bacfbf', //Your Azure Active Directory client id for accessing your Azure Maps account. | |
getToken: function (resolve, reject, map) { | |
//URL to your authentication service that retrieves an Azure Active Directory Token. | |
var tokenServiceUrl = 'https://azuremapscodesamples.azurewebsites.us/Common/TokenService.ashx'; | |
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 and point to the US Azure Gov Cloud domain. | |
searchURL = new atlas.service.SearchURL(pipeline, 'atlas.azure.us'); | |
//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, { | |
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><h1 style="font-size:16px">Page through POI results</h1></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> |