Devhammer's Den


Oct 8, 2012

W8WIL #6: Leverage the Maps app for location-based features

I'm a big fan of reuse wherever possible, so in this 6th installment of my Windows 8: What I've Learned series, I'm going to share a tip on how you can essentially get some great features for your app, with very little effort, by leveraging an app that ships with every copy of Windows 8.

The Maps app

Windows 8 machines will ship with several handy apps included, such as the Mail app, the People app and a few others. One of the more useful apps is the Maps app. It can, with the user's permission, use location features built into the machine (GPS, or network-based location services) to find your current location, integrated search for finding a desired address or point of interest, and built in support for directions, traffic, etc. You can see a screenshot of the maps app below:

Maps_App

But even cooler, from a developer perspective, is the fact that the Maps app supports protocol activation, which means you can programmatically invoke the Maps app from your own Windows Store app, using a special url syntax. Here's a video that shows how it works in my Community Megaphone app:

Creating the Url

The best part is how simple it is to do…you can just create an anchor element, and set its href attribute to a url beginning with "bingmaps:" and using one of several possible parameters to pass the location you wish to show. The official docs for the Maps app uri scheme, including all of the available parameters, can be found at:

http://msdn.microsoft.com/en-us/library/windows/apps/jj635237.aspx

In my case, I'm simply using the "where" query and passing it the address for the event I want directions to, and passing that value to the href attribute of my anchor element:

   1:  if (item.latlong) {
   2:      element.querySelector("article .item-directions").href = "bingmaps:?where=" + item.address + ", " 
   3:          + item.city + ", " + item.state;
   4:  }

Note that since not all events in Community Megaphone have addresses (for example, webcasts or online user groups), I first check to see whether the item has a lat/long value associated with it. If not, I don't show the "Get Directions" link at all (that code isn't shown above).

That's really all there is to it, and as you can see in the video above, this makes it really easy to add rich functionality around mapping and directions to your app, without having to build that functionality yourself. Once the href is set, clicking on the Get Directions link opens the Maps app, and you can get directions, as shown below:

Maps_Directions

Even cooler is that the Maps app supports printing the directions, so if someone wants to take their directions on paper (I know…very 20th century), they can simply use the Devices charm to send the directions to the desired printer (or any other configured device), as shown below:

Maps_Directions_Print

That's a lot of functionality that I didn't have to code.

Wrap-up

Given how cool protocol activation is, you might want to think about whether it makes sense to add it to your own Windows Store app. If there's useful functionality that your app exposes which other apps might find useful, registering to handle existing protocols (such as mailto:) or creating your own custom protocol for activation and publicizing it could result your app being used more often. And that's rarely a bad thing.

Generation App

Do you have a great idea, and want help turning it into a great app? Then you should register for Generation App. The Generation App program includes a 4-week process to get you from idea to app, including links to all the right documentation, tips, videos, and more. You can also access 1:1 phone support to talk with a Microsoft engineer about technical or design questions you may have. So sign up today!

Tags: W8WIL, Windows 8, Tips and Tricks, Mapping

Comments powered by Disqus

Visitors

Disclaimer

The views expressed on this weblog are mine and do not necessarily reflect the views of my employer.
All postings are provided "AS IS" with no warranties, and confer no rights.

Unless otherwise noted, all code provided in this blog is copyright © G. Andrew Duthie, and licensed under the Microsoft Limited Public License (Ms-LPL). All rights reserved.



worldmaps