Azure Maps and Xamarin
Published: 2020-09-09 09:31:29

---
title: Azure Maps and Xamarin
published: true
description: 
tags: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/0bzq7k3ze2wka999gcbr.png
---

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b6889hsihorhdlixopsv.png)

> This article is part of [#AzureBacktoSchool](https://captainhyperscaler.com/2020/03/03/azure-back-to-school/). You'll find other helpful articles and videos in this Azure content collection. New articles are published every day from community members and cloud advocates in the month of September. Thank you [Dwayne Natwick](http://twitter.com/DwayneNcloud) for organizing this awesome idea!

**Azure Maps** is a platform for geospatial services that uses recent map data in order to provide geographic context for web and mobile applications with powerful functionality.

Azure Maps can be used in a wide range of applications:

* smart location and logistics
* route calculation and search
* transportation area
* store locations... 
* You can even access data related to traffic incidents.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qk4el7nmhjwhnw1zk0p8.png)

* There is also a time zone API, which is very useful if you are creating an app that is used for various parts of the world or for scheduling deliveries. It is important to know where a vehicle is and what time zone it is in, as well as the destination time zone, which can be very important when it comes to figuring out the arrival time. 
* It also has a geolocation API to do things like look up the location from an IP address.

It also goes above and beyond for safety. One of the great things about Azure maps without a doubt, the GDPR compliance.

There are two different ways to authenticate usage with Azure maps. One is key-based authentication, quite common in different applications. Azure Active Directory has also been added for much greater security, even the map blocks the data so that it is only available to certain users.

In order to access Azure Maps services, just go to the Azure portal and create an Azure Maps account.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kga9hgeo9d7e17y7cfr2.png)

Then you'll get an API Key that can be used in your requests:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/af3as95yej4lo1d8pssx.png)

These are some of the available services in Azure Maps:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p5kbqsov73ej5m05c3vz.png)

And we can access their functionality through a REST API! So let's use them in a Xamarin mobile application! (The project is available in [GitHub](https://github.com/icebeam7/AzureMapsDemo) by the way)

Azure Maps provides the backend for these analysis services without a visual component, but of course adding it can be quite useful. For Xamarin, I am using the Xamarin.Forms Maps Nuget package.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/66pj7k6logorarswqxmq.png)

For the first demo, I'll create a very simple UI. A map with a couple of SearchBars.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nwgvzhrh3s5ffqgvmehu.png)

Then, in the code:

1. The first search retrieves the GPS coordinates of a given address, while the second one gets nearby specific points of interest. I also added the API Key from the Azure Maps Account. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6zy90hpf7y6xakjqc38v.png)

2. This is the GetLatLongByAddress method. We are using the *https://atlas.microsoft.com/search/address/json?api-version=1.0* endpoint, which requires two parameters: subscription-key (which we have) and query (the address). Then, if our request is successful, we add a pin in our map with the latitude and longitude we got from Azure Maps service.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/imm4yc2v11irl1cyq882.png)

(To convert the JSON string we obtain from Azure Maps service, I prepared a GeoCodingInfo class that extracts the info we need. You can check the model definition in the GitHub repository).

3. Now, let's take a look at the GetPois method, which finds nearby POIs from the location found in the previous method. The search string is what we would like to look for, like restaurants, libraries, schools, etc.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gtkm31n46f4htqktlo95.png)

As you can see, we are using the *https://atlas.microsoft.com/search/fuzzy/json?api-version=1.0* endpoint, which requires a subscription key and query. We provide place, lat and lon for this search. The PointsOfInterest class is created by us in order to deserialize the JSON response and show the results in a map (with pins). Check GitHub for the model definition.

5. Let's see the results! Firstly, let's find the coordinates of an address:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cnd7exbon0sh2boqtix0.png)

It worked! Now, let's look for some closer schools:

Again, we got results!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kn6ftn3yglvjjo48ilgx.png)

(By the way *Gymnázium* isn't what you might think ;-) In Czech, the secondary schools are called gymnáziums)

And that's it! As you can see, it is quite easy to start playing with Azure Maps in mobile applications with Xamarin. All you need is:

* The Azure Maps account (and key)
* Define your models 
* Send requests to the REST endpoints
* Deserialize the response and use them as you wish in your map!

Thank you for your time and hopefully this post was useful for you (let me know your thoughts in the comment section :-D). If you want to learn more about Azure, Xamarin, Artificial Intelligence and more, visit [my blog](https://luisbeltran.mx) and [YouTube](https://youtube.com/user/darkicebeam) channel, where I usually have fun sharing my knowledge and experiences.

Happy coding!

Luis