Ruprect8696

Had to do this for a project here at work and thought I would share the code:

{
var locs = new Array();
var lat1 = latin * Math.PI/180.0;
var lon1 = lonin * Math.PI/180.0;
var x;
for (x = 0; x <= 360; x++)
{
var tc = (x / 90)* Math.PI / 2;
var lat = Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc));
lat = 180.0 * lat / Math.PI;
var lon;
if (Math.cos(lat1)==0)
{
lon=lonin; // endpoint a pole
}
else
{
lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d)/Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
}
lon = 180.0 * lon / Math.PI;
var loc = new VELatLong(lat,lon);
locs.push(loc);
}
var poly = new VEPolyline(999, locs, new VEColor(0,255,0,.5) , 4);
return poly;
}

### Re: Draw a circle given a lat/long and radius (in miles)

Caleb T - MSFT

Very nice!

(and now you can change the second-to-last line to a VEPolygon object and add a fill color, if you want the circle filled in. )

### Re: Draw a circle given a lat/long and radius (in miles)

SoulSolutions

thanks Ruprect8696.

### Re: Draw a circle given a lat/long and radius (in miles)

MaxiWheat

Wow, awsome, thanks a lot ! Very useful

### Re: Draw a circle given a lat/long and radius (in miles)

Danny Thorpe MSFT

Nice!

### Re: Draw a circle given a lat/long and radius (in miles)

Federico Raggi - MSFT

I'm building the HowToPedia, a directory of FAQ (Frequently Asked Question) with references to the best/most popular questions and answers on the Windows Live Development forums and other places.

I added en antry for Ruprect8696's post. This is the first time we add an entry on the HowToPedia for a post on this forum.

You can view the HowToPedia entry for this post here: http://howtopedia.spaces.live.com/blog/cns!A1A77DBB3876417A!147.entry.

More HowToPedia entries: http://howtopedia.spaces.live.com/

### Re: Draw a circle given a lat/long and radius (in miles)

Ruprect8696, this piece of code has been very useful. Just had a question why are you dividing the radius by 3956. Just wanted to check because the circle that comes up on the map and the scale bar on the VE map(right bottom corner) doesnot match. When i draw a circle with radius 50miles that should cover approx. my entire map area if the scale bar also shows 50miles. But that doesnot happen.

### Re: Draw a circle given a lat/long and radius (in miles)

SoulSolutions

I had a chance to play a bit with this today and it seems like it may have issues with the whole "earth is round" problem. It all seems accurate for a radius less the a few hundred km, but any larger....

http://www.soulsolutions.com.au/portals/0/ve/testcircle.htm

Look at it in 3D mode.

I understand that in 2D mode the circles should look like that but in 3D mode should they not be round Or is there still a projection in play

The circle code is here:

http://www.viavirtualearth.com/wiki/Draw+a+circle.ashx

John.

### Re: Draw a circle given a lat/long and radius (in miles)

Ruprect8696

Because that is what the formula I found on the "internets" said. Honestly, I only tested it for what I would have the use for.

I just ran a quick check using Cleveland as the center point and Ashland as the end point (between 50 and 60 miles away) and I drew a circle for 60 mile radius. Sure enough Ashland was near the edge of the circle so at least for a range in the double-digit miles it seems accurate.

### Re: Draw a circle given a lat/long and radius (in miles)

Ruprect8696

I think I remember something about the circle formula being accurate only up to a certain radius (something like a hemisphere or something) but my application restricts the radius to double-digit mileage because of the amount of data that would be retrieved in a larger radius so it wouldn't affect me.

As I stated in a previous post the formula was pulled of the web so if someone feels like tweaking it to make it more accurate go ahead.

### Re: Draw a circle given a lat/long and radius (in miles)

Ruprect8696

Just for fun I drew 2 identical maps...one in 2d and one in 3d and it looks like the circle touches the same points on the map at 999 miles.

Can you do a side-by-side of both maps and see if the circle touches different points on the map

### Re: Draw a circle given a lat/long and radius (in miles)

SoulSolutions

Yeah I've read the same thing. The reality is most cases you looking for several miles (<100) anyway. Your code works well.

I'll make a note in the wiki entries. My understanding is there is a much more advanced forumula that is more accurate but a lot more work, It was way too confusing for me today.

Maybe someone else can add to the wiki entry

Thanks

John.

### Re: Draw a circle given a lat/long and radius (in miles)

Duncan Lawler

If it was me, I'd do it 3D space, it's pretty simple. Assume the earth is a sphere (it isn't, but is close). Convert your lat/long to a normalized 3D vector. Cross this vector with a vector to one of the poles to get a rotation axis. Rotate the original vector about this axis by your distance divided by the average radius of the earth (gives the radius of your circle in radians). That gives you a vector to a point on your circle. Now rotate this vector around the original vector in small increments to get other points. That should give you reasonable accuracy, and won't have any issues with the poles like the original sample code.

You can use a quaternion to do the rotations about axes easily. I have the C# code to do this, but not jscript.

### Re: Draw a circle given a lat/long and radius (in miles)

SoulSolutions

Ah the old normalized 3D vector! I would be really interested in the c# code. But.. does it use some libraries or functions that don't exist in javascript

John.

### Re: Draw a circle given a lat/long and radius (in miles)

Duncan Lawler

No special libraries, It's just simple math so it should be portable to jscript. The code will be a lot less clean since you don't have real classes and operator overloading.

I'll try and extract the relevant code and post when I get a chance.