Using Azure Maps with a Xamarin Forms app Part 3 – Route Directions

This is the third in a three part series where I build a Xamarin Forms app that uses the Azure Maps service. You can check out the other parts here:

Part 1 – Basic text search
Part 2 – Search by Category
Part 3 – This post.  Routing

In this post I’ll show you how I was able to query the Azure Maps Route Directions Endpoint and render the route on a map.  The route directions API endpoint takes a number of parameters, of which I only use a few.  Mainly, the source location lat and long, and the destination location lat and long.  The URI I construct ends up looking like this

For this demo, I let the user choose from a few hard coded source and destination locations.  For each preconfigured source and destination, I looked up the latitude and longitude, and stored them with the option.  When the user selects from one of the source and destination pairs, I pass the associated latitude/longitude coordinates to the API.  This is just a demo, so a real solution could be more dynamic by allowing the user to query for directions based on points of interest also returned by the API.  For this post, I’m returning the route for Stanford University to Pizzeria Delfina in Palo Alto, California.  (Palo Alto is the default simulated location for the Android Emulator).  Here’s a screenshot of the view where a user selects from one of the locations.  Its just a few buttons.

Here’s the resulting route drawn as an overlay on the phone’s native map. I was able to draw the polyline overlay using this technique for Xamarin Forms Maps.

Here’s what the code looks like for when the user clicks a button and the view loads to query the API based on the hard coded choice selection and render the map. (pay no attention to the async void lol)

To start I’m using a switch statement based on the user’s selection and passing the hard coded lat long to the API. Notice I’m instantiating the map AFTER I get the results back from the query. This was because of the way the overlay is drawn on to the map for Xamarin Forms. It involves creating a custom renderer which listens to when the map is instantiated. At first, the overlay wasn’t drawing on the map because I was instantiating the map before making the API call, and the listener on the custom renderer was running and rendering the map before the results of the route query were returned. This is one of the rare cases where I personally declare UI in code, to control the declaration timing at run time. After I instantiate the map I place a pin on the starting point no matter which route is chosen. Then I place a pin on the selected destination.

So there you have it. I’ve always been a sucker for POI, mapping etc so I’m excited to be able to explore the mapping API more.

Using Azure Maps with a Xamarin Forms app Part 2 – Search by Category

This is the second in a three part series where I build a Xamarin Forms app that uses the Azure Maps service. You can check out the other parts here:

Part 1 – Basic text search

Part 2 – This post.  Search by category
Part 3 – Routing

In this post, I’ll cover searching by category using the Get Search by Category API endpoint which allows you to search points of interest (POI) by a list of categories.   Calling the search by category service is actually pretty easy.  The endpoint URL is much like the text search endpoint, except for a couple different parts.  Here are the text search and category search URIs compared:

Text search (fuzzy)
https://atlas.microsoft.com/search/fuzzy/

Category search
https://atlas.microsoft.com/search/poi/category

I’m passing the required additional parameters and optional lat long for category search, so ultimately my URI call looks like this:

You need to pass your api key, a strongly recommended country set limiting parameter, the api version, return format (json), and a lat long.

In my Xamarin app, I built a simple page with a button upon which clicked, takes you to a page with a listview of categories.  When you click a category, I pass the selected category back to the previous page, perform the search and render the results as map pins on the map.

To make it more visually appealing, I’m using a mix of Font Awesome and Material Design icons in the category select page.  I was able to get the icons to render using the Iconize Plugin for Xamarin Forms   nuget packages.  It was a little tricky getting the packages to work as .NET Standard 2.0 is not supported on the latest stable versions, so I had to install the pre-release versions.  Ultimately I’m using the base Xam.Plugin.Iconize package two additional separate packages that each load a set of fonts, Xam.Plugin.Iconize.FontAwesme and Xam.Plugin.Iconize.Material

Here’s what my select category page looks like complete with icons:

Here are the results after you select a category.

Now to dig into the code details a little.  To make the category select page, I bind a listview to a category viewmodel

I create a list of this viewmodel and bind it to the list declared in my Xaml. The listview contains an item template that renders the icon as well as the display text of each category

Here’s the code to populate my list of viewmodel. You can see its a mix of Material Design and Font Awesome icons. The ‘fa-‘ and ‘md-‘ text triggers the ‘Iconize’ element to render from the icon set of Font Awesome and Material Design accordingly, based on whatever icons are included in the project by installing the nuget packages for that icon set.

When a category is chosen, I pass the selected category back to the referring page, make the API call and deserialize the Json into a model I created by pasting the Json into QuickType IO

Here’s what the code behind on that page looks like

You can see I’m attempting to grab the user’s location and pass it to the API along with the category selected.  That’s about it.  The Azure Maps service is proving to be pretty awesome.  I’ve been waiting for something like this for years.  It makes it easy to add location intelligence to your apps!  Happy coding.