Tutorial: Search nearby points of interest using Azure Maps
This tutorial shows how to set up an account with Azure Maps, then use the Maps APIs to search for a point of interest. In this tutorial, you learn how to:
- Create an Azure Maps account
- Retrieve the primary key for your Maps account
- Create a new web page using the map control API
- Use the Maps search service to find a nearby point of interest
Prerequisites
- Sign in to the Azure portal. If you don't have an Azure subscription, create a free account before you begin.
- Make an Azure Maps account
- Obtain a primary subscription key, also known as the primary key or the subscription key. For more information on authentication in Azure Maps, see manage authentication in Azure Maps.
Create a new map
The Map Control API is a convenient client library. This API allows you to easily integrate Maps into your web application. It hides the complexity of the bare REST service calls and boosts your productivity with customizable components. The following steps show you how to create a static HTML page embedded with the Map Control API.
On your local machine, create a new file and name it MapSearch.html.
Add the following HTML components to the file:
<!DOCTYPE html> <html> <head> <title>Map Search</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- 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> function GetMap(){ //Add Map Control JavaScript code here. } </script> <style> html, body { width: 100%; height: 100%; padding: 0; margin: 0; } #myMap { width: 100%; height: 100%; } </style> </head> <body onload="GetMap()"> <div id="myMap"></div> </body> </html>Notice that the HTML header includes the CSS and JavaScript resource files hosted by the Azure Map Control library. Note the
onloadevent on the body of the page, which will call theGetMapfunction when the body of the page has loaded. TheGetMapfunction will contain the inline JavaScript code to access the Azure Maps APIs.Add the following JavaScript code to the
GetMapfunction of the HTML file. Replace the string<Your Azure Maps Key>with the primary key that you copied from your Maps account.//Instantiate a map object var map = new atlas.Map("myMap", { //Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps authOptions: { authType: 'subscriptionKey', subscriptionKey: '<Your Azure Maps Key>' } });This segment initializes the Map Control API for your Azure Maps account key.
atlasis the namespace that contains the API and related visual components.atlas.Mapprovides the control for a visual and interactive web map.Save your changes to the file and open the HTML page in a browser. The map shown is the most basic map that you can make by calling
atlas.Mapusing your account key.
In the
GetMapfunction, after initializing the map, add the following JavaScript code.//Wait until the map resources are ready. map.events.add('ready', function() { //Create a data source and add it to the map. datasource = new atlas.source.DataSource(); map.sources.add(datasource); //Add a layer for rendering point data. var resultLayer = new atlas.layer.SymbolLayer(datasource, null, { iconOptions: { image: 'pin-round-darkblue', anchor: 'center', allowOverlap: true }, textOptions: { anchor: "top" } }); map.layers.add(resultLayer); });In this code segment, a
readyevent is added to the map, which will fire when the map resources have been loaded and the map is ready to be accessed. In the mapreadyevent handler, a data source is created to store result data. A symbol layer is created and attached to the data source. This layer specifies how the result data in the data source should be rendered. In this case, the result is rendered with a dark blue round pin icon, centered over the results coordinate, and allows other icons to overlap. The result layer is added to the map layers.
Add search capabilities
This section shows how to use the Maps Search API to find a point of interest on your map. It's a RESTful API designed for developers to search for addresses, points of interest, and other geographical information. The Search service assigns a latitude and longitude information to a specified address. The Service Module explained below can be used to search for a location using the Maps Search API.
Service Module
In the map
readyevent handler, construct the search service URL by adding the following JavaScript code.// Use SubscriptionKeyCredential with a subscription key var subscriptionKeyCredential = new atlas.service.SubscriptionKeyCredential(atlas. getSubscriptionKey()); // Use subscriptionKeyCredential to create a pipeline var pipeline = atlas.service.MapsURL.newPipeline(subscriptionKeyCredential); // Construct the SearchURL object var searchURL = new atlas.service.SearchURL(pipeline);The
SubscriptionKeyCredentialcreates aSubscriptionKeyCredentialPolicyto authenticate HTTP requests to Azure Maps with the subscription key. Theatlas.service.MapsURL.newPipeline()takes in theSubscriptionKeyCredentialpolicy and creates a Pipeline instance. ThesearchURLrepresents a URL to Azure Maps Search operations.Next add the following script block to build the search query. It uses the Fuzzy Search Service, which is a basic search API of the Search Service. Fuzzy Search Service handles most fuzzy inputs like addresses, places, and points of interest (POI). This code searches for nearby Gasoline Stations within the specified radius of the provided latitude and longitude. A GeoJSON feature collection from the response is then extracted using the
geojson.getFeatures()method and added to the data source, which automatically results in the data being rendered on the map via the symbol layer. The last part of the script sets the maps camera view using the bounding box of the results using the Map's setCamera property.var query = 'gasoline-station'; var radius = 9000; var lat = 47.64452336193245; var lon = -122.13687658309935; searchURL.searchPOI(atlas.service.Aborter.timeout(10000), query, { limit: 10, lat: lat, lon: lon, radius: radius }).then((results) => { // Extract GeoJSON feature collection from the response and add it to the datasource var data = results.geojson.getFeatures(); datasource.add(data); // set camera to bounds to show the results map.setCamera({ bounds: data.bbox, zoom: 10 }); });Save the MapSearch.html file and refresh your browser. You should see the map centered on Seattle with round-blue pins for locations of gasoline stations in the area.

You can see the raw data that the map is rendering by entering the following HTTPRequest in your browser. Replace <Your Azure Maps Key> with your primary key.
https://atlas.microsoft.com/search/poi/json?api-version=1.0&query=gasoline%20station&subscription-key={Your-Azure-Maps-Primary-Subscription-key}&lat=47.6292&lon=-122.2337&radius=100000
At this point, the MapSearch page can display the locations of points of interest that are returned from a fuzzy search query. Let's add some interactive capabilities and more information about the locations.
Add interactive data
The map that we've made so far only looks at the longitude/latitude data for the search results. However, the raw JSON that the Maps Search service returns contains additional information about each gas station. Including the name and street address. You can incorporate that data into the map with interactive popup boxes.
Add the following lines of code in the map
readyevent handler after the code to query the fuzzy search service. This code will create an instance of a Popup and add a mouseover event to the symbol layer.// Create a popup but leave it closed so we can update it and display it later. popup = new atlas.Popup(); //Add a mouse over event to the result layer and display a popup when this event fires. map.events.add('mouseover', resultLayer, showPopup);The API
*atlas.Popupprovides an information window anchored at the required position on the map.Add the following code within the
GetMapfunction, to show the moused over result information in the popup.function showPopup(e) { //Get the properties and coordinates of the first shape that the event occurred on. var p = e.shapes[0].getProperties(); var position = e.shapes[0].getCoordinates(); //Create HTML from properties of the selected result. var html = ` <div style="padding:5px"> <div><b>${p.poi.name}</b></div> <div>${p.address.freeformAddress}</div> <div>${position[1]}, ${position[0]}</div> </div>`; //Update the content and position of the popup. popup.setPopupOptions({ content: html, position: position }); //Open the popup. popup.open(map); }Save the file and refresh your browser. Now the map in the browser shows information popups when you hover over any of the search pins.

To view the full code for this tutorial, click here. To view the live sample, click here
Clean up resources
There are no resources that require cleanup.
Next steps
The next tutorial demonstrates how to display a route between two locations.