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