Over a million developers have joined DZone.

Drawing Accuracy Circle in Bing Maps on WP7

DZone's Guide to

Drawing Accuracy Circle in Bing Maps on WP7

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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"
                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)
    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);

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.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.


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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}