Mobile Zone is brought to you in partnership with:

My name is Toni Petrina and I am a software developer and an occasional speaker. Although I primarily develop on the Microsoft stack, I like to learn new technologies. My hobbyist projects range from game development, regardless of the technology, to ALM. I spend most of my time with my girlfriend and someday I will learn how to play the guitar properly. Toni is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Drawing Accuracy Circle in Bing Maps on WP7

06.21.2012
| 1901 views |
  • submit to reddit

While developing a location-based application, I was experiencing strange problems with the GPS service accuracy. My location varied wildly all over the place and I needed a better way to visualize my location. Along with drawing a marker on the map indicating where I am, I also a wanted to draw a circle around it to see what is the accuracy threshold for it.

When GeoCoordinateWatcher sends a notification about a new position, you can retrieve the horizontal accuracy information via the GeoCoordinate.HorizontalAccuracy property. Since there is no circle or ellipse shape for map, you can approximate it with MapPolygon. I’ve declared it in the following way:

<my:MapPolygon Fill="#99FF0000" Stroke="Red"
                StrokeThickness="1"
                Opacity="0.6"
                x:Name="precision" />

 The code is adapted from Steve Strong’s blog post. The full code for drawing a circle:

private void UpdatePrecision(GeoCoordinate location)
{
    precision.Visibility = Visibility.Collapsed;
    if (location == null)
        return;
 
    precision.Visibility = Visibility.Visible;
    precision.Locations = new LocationCollection();
 
    var earthRadius = 6371;
    var lat = location.Latitude * Math.PI / 180.0; //radians
    var lon = location.Longitude * Math.PI / 180.0; //radians
    var d = location.HorizontalAccuracy / 1000 / earthRadius; // d = angular distance covered on earths surface
 
    for (int x = 0; x <= 360; x++)
    {
        var brng = x * Math.PI / 180.0; //radians
        var latRadians = Math.Asin(Math.Sin(lat) * Math.Cos(d) + Math.Cos(lat) * Math.Sin(d) * Math.Cos(brng));
        var lngRadians = lon + Math.Atan2(Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat), Math.Cos(d) - Math.Sin(lat) * Math.Sin(latRadians));
 
        var pt = new GeoCoordinate(180.0 * latRadians / Math.PI, 180.0 * lngRadians / Math.PI);
        precision.Locations.Add(pt);
    }
}

On the image below you can see it in action.

Since emulator is very “precise”, I had to zoom in a bit. In regular usage the accuracy will vary.

Published at DZone with permission of Toni Petrina, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)