Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



Virtual Earth: Draw a Circle Radius Around a Lat/Long Point

I get requests on how to draw radius' around points on the map. And, up until now, I never needed to do it myself, so I didn't have a code snippet to do it. I did a search and quickly found an example over at viavirtualearth on how to do it in an older version of Virtual Earth. Other than being coded for an older version of Virtual Earth (and incompatible with VE6); it's coded to only handle drawing a radius in Kilometers.

So, I decided to upgrade the code example to support VE6, and support both Miles and Kilometers. I'm also converted it to make use of the GeoCodeCalc.ToDegrees function that I originally posted in my "Calculate Distance of User-Drawn Polyline" post.

Download the Full Example Here

And, in case you don't feel like downloading the example code that uses it, here's the source to the method that calculates the points that make up the radius: 

[code:js]
/* *************************************************************************** */
/* Written Chris Pietschmann (http://pietschsoft.com) */
/* This code is dependant on the GeoCodeCalc class found here: */
/* http://pietschsoft.com/Blog/Post.aspx?PostID=1453 */
/* *************************************************************************** */
/* This mathimatical code is a modified version of the code originally posted */
/* at the following location: *//* http://viavirtualearth.com/Wiki/Draw+a+circle.ashx */
/* *************************************************************************** */
if (GeoCodeCalc == undefined)
    var GeoCodeCalc = {}

GeoCodeCalc.ToDegrees = function(radians){ return radians * 180 / Math.PI;};

function CreateCircle(loc, radius, units){
var earthRadius = parseFloat(units);
var lat = GeoCodeCalc.ToRadian(loc.Latitude); //radians
var lon = GeoCodeCalc.ToRadian(loc.Longitude); //radians
var d = parseFloat(radius) / earthRadius; // d = angular distance covered on earth's surface
var locs = new Array();
for (x = 0; x <= 360; x++) {
var p2 = new VELatLong(0,0)
brng = GeoCodeCalc.ToRadian(x); //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));
locs.push(new VELatLong(GeoCodeCalc.ToDegrees(latRadians), GeoCodeCalc.ToDegrees(lngRadians)));
}
return new VEShape(VEShapeType.Polyline, locs);
}
[/code]

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by crpietschmann on Saturday, February 09, 2008 2:58 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann