DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Azure Maps and Xamarin
Luis Beltran
Luis Beltran

Posted on

Azure Maps and Xamarin

Alt Text

This article is part of #AzureBacktoSchool. 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 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

  • 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

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

Alt Text

These are some of the available services in Azure Maps:

Alt Text

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 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

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

Alt Text

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

  1. 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

(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).

  1. 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

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.

  1. Let's see the results! Firstly, let's find the coordinates of an address: Alt Text

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

Again, we got results!

Alt Text

(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 and YouTube channel, where I usually have fun sharing my knowledge and experiences.

Happy coding!

Luis

Top comments (0)

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.