How to draw a circle with a radius in kilometers

Showing 1-1 of 1 messages
How to draw a circle with a radius in kilometers Morta 3/28/11 3:46 AM
How to draw a circle with a radius in kilometers around a coordinate using the API
Understandably, Google is not successfully resolved this problem out of Ecuador (parallel 0) in the examples of the API and in other places, the circle becomes an oval
I think this is a possible solution:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Build a circle of radius in km Google Earth</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Miguel Ortega" />
<script src="http://www.google.com/jsapi?key=YOUR_API_KEY"></script>
<script type="text/javascript">
google.load("earth", "1");
var ge = null;

function init() {
google.earth.createInstance("map3d", initCB, failureCB);
}

function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getOptions().setStatusBarVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);


var lookAt = ge.createLookAt('');
lookAt.setLatitude(42.32331619631114);
lookAt.setLongitude(2.598912324753657);
lookAt.setRange(400000.0);
ge.getView().setAbstractView(lookAt);


createMultiGeometry();
}
function failureCB(object) {
alert('Error');
}
var lat1="42.32331619631114";
var lon1="2.598912324753657";
var radio="25000";//meters
var pi = Math.PI;

lat1 = lat1 * pi / 180;
lon1 = lon1 * pi / 180;
//radius WGS 84 http://en.wikipedia.org/wiki/Figure_of_the_Earth
var d_radio = radio/6378137;

function createMultiGeometry() {
function draw_circle() {
var center = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
var ring = ge.createLinearRing('');
var radial;
var lat_rad;
var dlon_rad;
var lon_rad;
for (var i = 0; i <= 360; i++) { //if we vary the initial value of i and / or grade, you get different figures
radial = i * pi / 180;
lat_rad = Math.asin(Math.sin(lat1)*Math.cos(d_radio) + Math.cos(lat1)*Math.sin(d_radio)*Math.cos(radial));
dlon_rad = Math.atan2(Math.sin(radial)*Math.sin(d_radio)*Math.cos(lat1),Math.cos(d_radio)-Math.sin(lat1)*Math.sin(lat_rad));
lon_rad = parseFloat((lon1+dlon_rad + pi), 2*pi) - pi;
lon_rad=lon_rad * 180 / pi;
lat_rad=lat_rad * 180 / pi;
ring.getCoordinates().pushLatLngAlt(lat_rad, lon_rad, 0);
}
return ring;
}

var multGeoPlacemark = ge.createPlacemark('');
multGeoPlacemark.setGeometry(ge.createMultiGeometry(''));
multGeoPlacemark.setStyleSelector(ge.createStyle(''));
var lineStyle = multGeoPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(3);
lineStyle.getColor().set('ff0000FF');
var geoms = multGeoPlacemark.getGeometry().getGeometries();
geoms.appendChild(draw_circle());
ge.getFeatures().appendChild(multGeoPlacemark);
}
</script>
</head>
<body onload='init()' id='body'>
<div id='map3d_container' style='border: 5px solid silver; height: 550px;'>
<div id='map3d' style='height: 100%;'></div>
</div>
</body>