Over a million developers have joined DZone.

Drawing Accuracy Circle in Bing Maps on WP7

· Mobile Zone

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.

Topics:

Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}