Coordinates, Doubles and a Localisation Bug

I was recently driven crazy by a particularly annoying bug whilst developing a feature of Foundbite that allows you to view what other’s have uploaded in a certain location.

In the app you scroll over the area in the world you want to view and the app then sends a simple request to our Azure hosted API to get the foundbites in a given location rect. The request looks a bit like this:

It’s constructed by getting the coordinates and simply using double.ToString() for each individual lat/long.

Testing on my Windows Phone device it all worked fine but a select group of beta testers reported that whenever they visited the page and tried to load some bites the map was remaining blank and the web request was failing every time.

This was confusing to say the least and I spent a whole day trying to work out what was wrong, including trying different versions of the app but came up empty.

At my wits end (see above) I consulted with a fellow Windows Phone developer, George Spyrou, who had incidentally fixed the exact same bug in his app that very day.

It turns out, all the users that were reporting this bug were based in European countries and had different cultures on their phones. These cultures use a “,” instead of a “.” in decimal values and therefore when the app was composing the URL in these cultures it looked like this:,2&lonMin=-179,232&latMax=79,343&lonMax=179,99

It doesn’t look all the different if you can glance at it, but the commas in the URL invalidate it and the request will return an error every time without giving you much of an idea as to why.

Thankfully when I became aware of it, there was a simple fix at hand; instead of using double.ToString() you should use double.ToString(CultureInfo.InvariantCulture) which ensures the culture is ignored.

A simple bug but hopefully this’ll prevent other devs falling into a similar trap when incorporating GPS Coordinates into URL’s.


Lessons learnt working with pushpins and maps in Windows Phone 8

Given the location-based nature of Foundbite, I’ve had to do a lot of work with pushpins and maps. When I was first starting out I ran into a couple of hurdles with the difference between WP8 and WP7.

Adding a pushpin to a map I can’t remember how exactly I came about this method of adding pushpins (or if it’s completely necessary) but I’ve been adding MapLayers to the map which include several MapOverlay’s each containing objects you want to display at a certain location (in this case pins). Pushpins do have a location property you can set but using a MapOverlay means you can add anything.

MapLayer layer = new MapLayer();
Pushpin pp = new Pushpin();
MapOverlay overlay = new MapOverlay();
overlay.Content = pp;

Setting the overlay (and therefore the pushpin’s) location

You just need to select the correct MapOverlay from the layer. Easy.

layer[x].GeoCoordinate = new GeoCoordinate(Latitude, Longitude);

Changing the pushpin’s style

If you want to change the style of your pushpin, it’s as easy as using the editor in Blend, saving to Application resources in App.xaml and then calling this line of code:

pp.Style = App.Current.Resources["MyStyle"] as Style

Changing the position origin of your pushpin

If you edited the style of your pushpin you might not want it to be placed on the map by its bottom left corner as normal. This can be changed via the PositionOrigin property of the overlay:

overlay.PositionOrigin = new Point(0.5, 1);

This sets the position origin to the bottom middle of the pushpin. (0,0) is the top left.

Get the position of a tap on the map to add a pushpin

Definitely one of the most useful and least used parts of the map control is the ability to get the geo location of a user’s tap on the map. You just need to get the screen coordinate of the click in relation to the map control (measured from the top left again) and then use the handy map method below to convert this to a GeoCoordinate.

var point = e.GetPosition(map);
var location = map.ConvertViewportPointToGeoCoordinate(point);

There’s also ConvertGeoCoordinateToViewportPoint which does the reverse.

I discovered this method after writing a version of the ConvertViewportPointToGeoCoordinate myself over the course of the weekend – so much time wasted!

Hopefully these are useful!

This is a a copy of a post I wrote on my previous blog: