Calculating Distance In Bing Maps
This weekend I was feeling crappy so I wrote an Appy
. Where I was not really crappy but I wanted to write another app to learn more about the Map control in Windows Phone. I will post about the app some other time but wanted to share this piece of code I think might come in handy.
The app I wrote basically allow the user to set a location and a “geo fence” around it. The user get a warning when they have reach the radios of the “fence”. To do this I needed to calculate the the distance a pixel and a map represented. Looking at the documents, the Scale class in the Maps Overlay namespace seems to to provide a MetersToPixel property bit I was not able to get any info out of it. So I wrote this once. Bing Maps Control has a Zoom Level property. Each level represents and MetersPerPixel so I attached to that.
Here is the code:
static private double[] MapsScaleFactor = { 78271.52, 78271.52, 39135.76, 19567.88, 9783.94, 4891.97, 2445.98, 1222.99, 611.50, 305.75,152.87, 76.44, 38.22, 19.11, 9.55, 4.78, 2.39, 1.19,0.60, 0.30 }; static public double MetersPerPixel(double ZoomLevel) { if (ZoomLevel < 0 || ZoomLevel > (MapsScaleFactor.Length - 1)) return MapsScaleFactor[0]; else return MapsScaleFactor[(int)Math.Round(ZoomLevel)]; }
My name is Dani Diaz. I work for Microsoft as a Developer Evangelist covering the Northern East Coast. Oh and before I forget. The opinions express on this site are my owns and not that of my employer.



