Launch Maps from any Metro app, Windows 8
BREAKING:Here is update from Windows 8 Maps (Michael Malueg and Luis Cabrera speaking):
Officially documented URI scheme is available at following location: http://msdn.microsoft.com/en-us/library/windows/apps/jj635237.aspx
As Michael says, “Feel free to leave comments and feedback about the URI scheme there“.
Ideally, would be great if there exists same task as on Windows Phone. Some kind of Map Launch application.
I tried searching, but nothing found.
Some people were pointing to Bing Maps SDK for Metro apps, but this would end in adding dedicated page with map control and duplicating most functionality of Maps app. I don’t understand why I (and every other developer) should spend time in writing it’s own Maps app.
Other people suggest using protocol activation feature. This is customizable and powerful feature allowing communicating between different apps.
Only issue with protocol activation is that it’s unclear which protocol should I activate to run Maps app and how to send there exact location I need to view.
Now I’d like to share my solution.
Let’s start from the basics. One can view registered protocols in Windows using ‘Control Panel\Programs\Default Programs’ selecting ‘Associate a file type or protocol with a program’
Searching through available protocols shows me two url schemas registered to Maps application! Yahoo!
Let’s see how we can use that information.
To use protocol activation, let’s see HOWTOs for Windows 8. Here is one which shows how to activate default app for a protocol. You can also take a look at downloadable sample.
string uriToLaunch = @"http://www.bing.com";
var uri = new Uri(uriToLaunch);
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
Remember, we should use schema
‘maps‘ or ‘bingmaps‘. Creating Uri from string “bingmaps://55.756486,37.617188” fails because Uri type needs host name. Let’s better use “bingmaps://open/55.756486,37.617188“. Using that string Maps application is now opened, but it doesn’t focus on location I specified in string (55.756486,37.617188 is somewhere in Moscow)
upd: at Windows 8 RTM there is the only protocol for Bing Maps and now you should use “bingmaps” schema only.
Let’s dig some more details. Launch Task manager and open Maps app location.
XAMLs? That might be our lucky hour. Checking if it’s .NET app with ILSpy and it’s written using .NET for Metro
Looking through the code points me to the class ProtocolParser which does all the work with protocol and Uri supplied.
Okay, next minutes I spend analyzing the code. To cut long story short, here what I found:
- All information is taken from Uri query string. That means that word between maps:// and ? is not even taken into account. For Maps app Uri “bingmaps://wheretobuyretina/?key=Value” and “bingmaps://ineedwifisharingonwp7/?key=Value” are identical.
- There are parameters to setup map view and to run query (search for objects, build directions).
- Supported parameters for configuring view are following:
- “cp=55.756486~37.617188” centers view at point with latitude=55.756486 and longitude=37.617188
- “lvl=15” sets zoom level to 15
- “sty=a” switches map to aerial view, “sty=r” switches map to road view
- “trfc=1” enables traffic information, “trfc=0” disables traffic information
- “bb=55.756486,37.617188,56,34” focuses map on bounding box with left-top coordinates 55.756486, 37.617188 and bottom-right coordinates 56,34
- Supported query parameters are following:
- “q=bar” searches for bars, you can supply location where to search using parameter “where=Moscow“
- “rtp=adr.Minsk~adr.Moscow” starts building route from Minsk to Moscow (before, Maps gets reverse geocode for terms ‘Minsk’ and ‘Moscow’)
- “rtp=pos.55.756486_37.617188~pos.56_34” starts building route from point 55.756486, 37.617188 to point 56, 34
Now you can use all power of Maps app in your Windows 8 application. I wrote a very simple class that builds that Uri string and opens Maps application.
You can see the code of MapsHelper or install using Nuget command “Install-Package Windows8.MapsHelper”
Example of usage:
MapsHelper.OpenMaps(lat: latitude, lon: longitude);
Well done! Hope that helps you building better apps.