Jimmy Engström

NAVIGATION

How to use BingMapsDirectionsTask

Mango introduces a new way to show directions called BingMapsDirectionsTask.

The usage is very simple:

 

using Microsoft.Phone.Tasks;


BingMapsDirectionsTask directions = new BingMapsDirectionsTask();
directions.Start= new LabeledMapLocation("start",new System.Device.Location.GeoCoordinate(59.3362,18.0710);
directions.End = new LabeledMapLocation("stop",new System.Device.Location.GeoCoordinate(59.3360, 18.0679);
directions.Show();

Quite simple huh? But there is a catch, the above code won’t work if you are using a regions format that uses comma instead of dot as a decimal separator.
Swedish is one of those. I have tried some different ways of solving that by doing a reverse geocoding with the bing services but it didn’t help.
I haven’t done a deep analysis of this but what I think happens is that the BingMapsDirectionsTask wont recognize the GeoCoordinates as a correct point and then uses the text I have supplied (in this case "start" and "stop") to try to find the address instead. But in this demo I don’t have the correct address, and IF I did it wouldn’t understand the special character of the swedish language (åäö) which will result in a box asking which address is the correct one, even though I know the correct GPS coordinates.

I don’t want to ask the user, the user won't know.
My guess is that Microsoft will fix this soon, but perhaps not in Mango when it releases.

What can we do? There is a quite simple solution to solve this. Before you call the directions.Show() you can set the current culture to en-US and set it back.
This is how I solved it:

string realCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
try
{
       BingMapsDirectionsTask directions = new BingMapsDirectionsTask();
    directions.Start= new new LabeledMapLocation("start",new System.Device.Location.GeoCoordinate(59.3362,18.0710);     directions.End = new LabeledMapLocation("stop",new System.Device.Location.GeoCoordinate(59.3360, 18.0679);
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    directions.Show();
}
catch { }
finally
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo(realCulture);
}

It will however still have problems with Swedish characters in LabeledMapLocation labels so you might want to avoid to supply special characters. In this particular case the important thing to me is that the direction task is working.

 

Posted in Windows Phone