Overview Of Azure Maps

In this article let’s take an overview of Azure Maps. The Azure Maps provides the geospatial capability for mapping the data. Since the Azure Maps are available for both Mobile and Web they are packed with the freshest mapping of data, and in addition to that the Azure maps offers the search, maps, routing, traffic and mobility, weather, etc. Before getting started on working with Azure maps, lets create an account.
 
Prerequisites
  • Azure Subscription
Step 1
 
Sign in to the online Microsoft Azure Portal.
 
Step 2
 
From the Azure portal menu, or from the Home page, select Create a resource. In the Search box, enter Maps. From the results list, choose Maps. Choose Create.
 
 
For creating the map account, we need to provide the following basic required values,
  • Provide a name for the account.
  • Select the resource group if you have any existing or you can create a new resource group.
  • Select the subscription and select the pricing tier for the account.
 
Finally, click on Create button for creating the Azure map account.
 
Step 3
 
Now we can see that we have successfully created the maps account and now we have to retrieve the primary key, the primary key is for enabling the querying of the map API’s. For retrieval of the primary keys Select >> Authentication.
 
Copy the primary key to your clipboard so that we can use it later.
 
 
Step 4
 
Here comes the main part, now we have to create a new map. For that, just create an html file in your local PC and place the following code inside of the html file.
 
What happens here is the MAP control API is a convenient library in which the API allows us to integrate maps into the web application, since the productivity will be increased due to the hidden complexity of the bare REST service calls. Now let’s get back to the process.
 
Let’s add some of the code inside the GetMap function in the html file. Make sure you have placed the primary key inside of the <your azure map key> tag.
 
Now if we run the html file in the browser, we can see the map is displayed in the browser by calling atlas.map using the account key.
 
Step 5
 
Let’s keep on proceeding to the next step, now add the following code in the GetMap (), after initializing the map 
  1. //Wait until the map resources are ready.  
  2. map.events.add('ready'function() {  
  3.   
  4.     //Create a data source and add it to the map.  
  5.     datasource = new atlas.source.DataSource();  
  6.     map.sources.add(datasource);  
  7.   
  8.     //Add a layer for rendering point data.  
  9.     var resultLayer = new atlas.layer.SymbolLayer(datasource, null, {  
  10.         iconOptions: {  
  11.             image: 'pin-round-darkblue',  
  12.             anchor: 'center',  
  13.             allowOverlap: true  
  14.         },  
  15.         textOptions: {  
  16.             anchor: "top"  
  17.         }  
  18.     });  
  19.   
  20.     map.layers.add(resultLayer);  
  21. });  
Since the ready event has been added to the map, it will be executed when the resources have been loaded and the map is ready to be accessed. It’s now time for adding the search capabilities. Let’s keep on proceeding to the next step, which is to use the Search API for finding a point of interest on the map for searching for  address and point of interest.
 
I have provided a location of Coimbatore and my point of interest is for searching for a  petrol-bunk in the geographical area. The search API searches for the address points by using the latitude and longitude information to a specified address. Add the following code inside of the map ready event handler. 
  1. // Use SubscriptionKeyCredential with a subscription key  
  2. var subscriptionKeyCredential = new atlas.service.SubscriptionKeyCredential(atlas.getSubscriptionKey());  
  3.   
  4. // Use subscriptionKeyCredential to create a pipeline  
  5. var pipeline = atlas.service.MapsURL.newPipeline(subscriptionKeyCredential);  
  6.   
  7. // Construct the SearchURL object  
  8. var searchURL = new atlas.service.SearchURL(pipeline);  
Step 6
 
The following script provides the search query for both latitude and longitude by using the Fuzzy Search Service which is based on Search API. The Fuzzy Search handles the inputs like address, pin-points and places. Now add the following JavaScript code to the search module.
  1. var query =  'petrol-bunk';  
  2. var radius = 9000;  
  3. var lat = 11.004556;  
  4. var lon = 76.961632;  
  5.   
  6. searchURL.searchPOI(atlas.service.Aborter.timeout(10000), query, {  
  7.     limit: 10,  
  8.     lat: lat,  
  9.     lon: lon,  
  10.     radius: radius  
  11. }).then((results) => {  
  12.   
  13.     // Extract GeoJSON feature collection from the response and add it to the datasource  
  14.     var data = results.geojson.getFeatures();  
  15.     datasource.add(data);  
  16.   
  17.     // set camera to bounds to show the results  
  18.     map.setCamera({  
  19.         bounds: data.bbox,  
  20.         zoom: 10  
  21.     });  
  22. });  
Now save the html file and refresh the browser, we can see that the given latitude and longitude have been mapped, and it’s time for adding interactive data so that the pin-point will be more effective and user friendly.
 
 
 
Step 7
 
For more interactive usage of the maps, add the following code inside of the map ready event handler and this code will create popup and a mouseover event. 
  1. //Create a popup but leave it closed so we can update it and display it later.  
  2. popup = new atlas.Popup();  
  3.   
  4. //Add a mouse over event to the result layer and display a popup when this event fires.  
  5. map.events.add('mouseover', resultLayer, showPopup);  
Again, add the following code inside the GetMap function so that the mouseover result will pop-up 
  1. function showPopup(e) {  
  2.     //Get the properties and coordinates of the first shape that the event occurred on.  
  3.   
  4.     var p = e.shapes[0].getProperties();  
  5.     var position = e.shapes[0].getCoordinates();  
  6.   
  7.     //Create HTML from properties of the selected result.  
  8.     var html = `  
  9.       <div style="padding:5px">  
  10.         <div><b>${p.poi.name}</b></div>  
  11.         <div>${p.address.freeformAddress}</div>  
  12.         <div>${position[1]}, ${position[0]}</div>  
  13.       </div>`;  
  14.   
  15.     //Update the content and position of the popup.  
  16.     popup.setPopupOptions({  
  17.         content: html,  
  18.         position: position  
  19.     });  
  20.   
  21.     //Open the popup.  
  22.     popup.open(map);  
  23. }  
 
Now again save the html file and just refresh the browser so that maps will pop-ups with pin-point locations and for your kind reference I have provided the entire source code in a  sequential manner. 
  1. <!DOCTYPE html>    
  2.     <html lang="en">    
  3.     <head>    
  4.         <title>Search for points of interest - Azure Maps Web SDK Samples</title>    
  5.         <meta charset="utf-8" />    
  6.         <meta http-equiv="x-ua-compatible" content="IE=Edge" />    
  7.         <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />    
  8.         <meta name="description" content="This tutorial shows how to search for points of interest and display them on the map." />    
  9.         <meta name="keywords" content="Microsoft maps, map, gis, API, SDK, services, module, tutorials, search" />    
  10.         <meta name="author" content="Microsoft Azure Maps" />    
  11.         
  12.         <!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->    
  13.         <link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" />    
  14.         <script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>    
  15.     
  16.         <!-- Add a reference to the Azure Maps Services Module JavaScript file. -->    
  17.         <script src="https://atlas.microsoft.com/sdk/javascript/service/2/atlas-service.min.js"></script>    
  18.         
  19.         <script>    
  20.             function GetMap() {    
  21.                     
  22.                 // Instantiate a map object    
  23.                 var map = new atlas.Map('myMap', {    
  24.                     view: 'Auto',    
  25.                         
  26.                     //Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps    
  27.                     authOptions: {    
  28.                         authType: 'subscriptionKey',    
  29.                         subscriptionKey: '<Your Azure Maps Key>'    
  30.                     }    
  31.                 });    
  32.         
  33.     
  34.                 //Wait until the map resources are ready.    
  35.                 map.events.add('ready'function () {    
  36.         
  37.     
  38.                     //Create a data source and add it to the map.    
  39.                     datasource = new atlas.source.DataSource();    
  40.                     map.sources.add(datasource);    
  41.         
  42.     
  43.                     //Add a layer for rendering point data.    
  44.                     var resultLayer = new atlas.layer.SymbolLayer(datasource, null, {    
  45.                         iconOptions: {    
  46.                             image: 'pin-round-darkblue',    
  47.                             anchor: 'center',    
  48.                             allowOverlap: true    
  49.                         },    
  50.                         textOptions: {    
  51.                             anchor: "top"    
  52.                         }    
  53.                     });    
  54.         
  55.     
  56.                     map.layers.add(resultLayer);    
  57.         
  58.     
  59.                     // Use SubscriptionKeyCredential with a subscription key    
  60.                     var subscriptionKeyCredential = new atlas.service.SubscriptionKeyCredential(atlas.getSubscriptionKey());    
  61.         
  62.     
  63.                     // Use subscriptionKeyCredential to create a pipeline    
  64.                     var pipeline = atlas.service.MapsURL.newPipeline(subscriptionKeyCredential);    
  65.         
  66.     
  67.                     // Construct the SearchURL object    
  68.                     var searchURL = new atlas.service.SearchURL(pipeline);    
  69.         
  70.     
  71.                     var query = 'petrol-bunk';    
  72.                     var radius = 9000;    
  73.                     var lat = 11.004556;    
  74.                     var lon = 76.961632;    
  75.         
  76.     
  77.                     searchURL.searchPOI(atlas.service.Aborter.timeout(10000), query, {    
  78.                         limit: 10,    
  79.                         lat: lat,    
  80.                         lon: lon,    
  81.                         radius: radius,    
  82.                         view: 'Auto'    
  83.                     }).then((results) => {    
  84.         
  85.     
  86.                         // Extract GeoJSON feature collection from the response and add it to the datasource    
  87.                         var data = results.geojson.getFeatures();    
  88.                         datasource.add(data);    
  89.         
  90.     
  91.                         // set camera to bounds to show the results    
  92.                         map.setCamera({    
  93.                             bounds: data.bbox,    
  94.                             zoom: 10,    
  95.                             padding: 15    
  96.                         });    
  97.                     });    
  98.         
  99.     
  100.                     //Create a popup but leave it closed so we can update it and display it later.    
  101.                     popup = new atlas.Popup();    
  102.         
  103.     
  104.                     //Add a mouse over event to the result layer and display a popup when this event fires.    
  105.                     map.events.add('mouseover', resultLayer, showPopup);    
  106.         
  107.     
  108.                     function showPopup(e) {    
  109.                         //Get the properties and coordinates of the first shape that the event occurred on.    
  110.         
  111.     
  112.                         var p = e.shapes[0].getProperties();    
  113.                         var position = e.shapes[0].getCoordinates();    
  114.         
  115.     
  116.                         //Create HTML from properties of the selected result.    
  117.                         var html = ['<div style="padding:5px"><div><b>', p.poi.name,    
  118.                             '</b></div><div>', p.address.freeformAddress,    
  119.                             '</div><div>', position[1], ', ', position[0], '</div></div>'];    
  120.         
  121.     
  122.                         //Update the content and position of the popup.    
  123.                         popup.setPopupOptions({    
  124.                             content: html.join(''),    
  125.                             position: position    
  126.                         });    
  127.         
  128.     
  129.                         //Open the popup.    
  130.                         popup.open(map);    
  131.                     }    
  132.                 });    
  133.             }    
  134.         </script>    
  135.         
  136.     
  137.         <style>    
  138.             html,    
  139.             body {    
  140.                 width: 100%;    
  141.                 height: 100%;    
  142.                 padding: 0;    
  143.                 margin: 0;    
  144.             }    
  145.        
  146.    
  147.             #myMap {    
  148.                 width: 100%;    
  149.                 height: 100%;    
  150.             }    
  151.         </style>    
  152.     </head>    
  153.         
  154.     <body onload="GetMap()">    
  155.         <div id="myMap"></div>    
  156.     </body>    
  157.         
  158.     </html>     
I hope this article will be useful to you and thanks for reading.