Hola Poligemo ¿que tal?
Hacía bastante que no programaba en GE y llevo tiempo detras de solucionar este tema pero no doy con ello. He encontrado otro ejemplo que parece que lo consigue también
earth-api-samples.googlecode.com/ ...index.html
He probado de otra forma (algunas de las cosas sobran pero como estoy intentando que funcione no las he quitado todavía)
Esto mas o menos hace lo que yo quiero pero el problema que tengo es que quisiera que el circulo fuesen 50 km alrededor del punto y lo mas que consigo aproximarme (y realmente no se si está bien) es un radio de 82 km y no se como cambiarlo
Este es el código, que está basado en el de
www.barnabu.co.uk/geapi/polyplot/
Un saludo y muchas gracias
------
<!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>PolyPlot - draw and compute areas of polygons on Google Earth</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ployplot, draw and calculate the area of regular polygons on the surface of the globe. Using the Google Earth Plugin" />
<meta name="author" content="James Stafford" />
<script src="http://www.google.com/jsapi?key=ABQIAAAArCZgeQ8ddm8YbeJQqqHN5BTYrZjdppbykE52CdH9dtH9wJ0VsBQSgm6ZbaTJsaMARyyYkz_JttmHMQ"></script>
<script type="text/javascript">
Var pi = Math.PI;
Function Polygon(lat,lon,lata,lona,sides) {
var me = this;
me.numsides = 120;
me.cent = new PM(lat,lon,'centre','ffff0000');
me.rad = new PM(lata,lona,'outer','ffff0000');
me.setBearDist();
var lineStringPlacemark = ge.createPlacemark('');
me.lineString = ge.createLineString('');
lineStringPlacemark.setGeometry(me.lineString);
me.lineString.setTessellate(true);
me.drawPolygon();
ge.getFeatures().appendChild(lineStringPlacemark);
lineStringPlacemark.setStyleSelector(ge.createStyle(''));
me.lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle();
}
Polygon.prototype.setBearDist = function() {
this.bear = bearing(this.cent.lat,this.cent.lon, this.rad.lat, this.rad.lon);
this.dist = distance(this.cent.lat,this.cent.lon, this.rad.lat, this.rad.lon);
document.getElementById('rad').innerHTML = this.dist.toPrecision(6).toString()+' km';
document.getElementById('bear').innerHTML = this.bear.toDeg().toPrecision(5).toString()+' deg';
}
Polygon.prototype.setRad = function() {
var latlon = destination(this.cent.lat,this.cent.lon, this.dist,this.bear);
this.rad.setLoc (latlon[0],latlon[1]);
}
Polygon.prototype.colour = function(col) {
this.lineStyle.getColor().set(col);
}
Polygon.prototype.drawPolygon = function() { // Draw our Polygon
var latlon;
this.lineString.getCoordinates().clear();
for (i=0; i <=this.numsides; i++) {
latlon = destination(this.cent.lat,this.cent.lon,this.dist,this.bear+i*2*pi/this.numsides);
this.lineString.getCoordinates().pushLatLngAlt(latlon[0],latlon[1],0);
}
this.areaCircum();
}
Function PM(lat,lon,name,colour) { // Create Placemark
var me = this;
me.active = false;
me.name = name;
me.placemark = ge.createPlacemark('');
ge.getFeatures().appendChild(me.placemark);
me.point = ge.createPoint('');
me.placemark.setStyleSelector(ge.createStyle(''));
var IconStyle = me.placemark.getStyleSelector().getIconStyle();
IconStyle.getColor().set(colour);
IconStyle.getHotSpot().setXUnits(ge.UNITS_FRACTION);
IconStyle.getHotSpot().setYUnits(ge.UNITS_FRACTION);
IconStyle.getHotSpot().setX(0.5);
IconStyle.getHotSpot().setY(0.5);
me.setLoc(lat,lon);
}
PM.prototype.setLoc = function(lat,lon) { // set location of placemark.
this.lat = lat.toRad();
this.lon = lon.toRad();
this.point.setLatLng(lat,lon.fixLon());
document.getElementById(this.name).innerHTML = lat.toPrecision(7).toString()+' , '+lon.toPrecision(7).toString();
}
Polygon.prototype.movePMLoc = function(kmlEvent) {
if (!this.rad.active && !this.cent.active) {
this.colour('ffff0000');
}
else {
kmlEvent.preventDefault();
this.colour('ffff0000');
if (this.rad.active) {
this.rad.setLoc (kmlEvent.getLatitude(),kmlEvent.getLongitude());
this.setBearDist();
this.drawPolygon();
}
else { // only pick up centre placemark if vertex placemark not selected
this.cent.setLoc (kmlEvent.getLatitude(),kmlEvent.getLongitude());
this.setRad();
this.drawPolygon();
}
}
}
Polygon.prototype.completelyNewLoc = function(kmlEvent) {
if(kmlEvent.getAltKey()){
this.cent.active = false;
this.rad.active = true;
this.cent.setLoc(kmlEvent.getLatitude(),kmlEvent.getLongitude());
this.rad.setLoc(kmlEvent.getLatitude(),kmlEvent.getLongitude());
this.colour('ffff0000');
}
}
PM.prototype.draw = function() {
this.active = true;
}
PM.prototype.undraw = function() {
this.active = false;
}
Polygon.prototype.areaCircum = function() { // compute area and circumference of Polygon
var area = 0;
var circum = 0;
var latlon;
if (this.numsides == 25){ // area of spherical circle = 2*pi*R^2*(1-cos(radius))
area = 2*pi*6371*6371*(1-Math.cos(this.dist/6371));
circum = 2*pi*6371*(Math.sin(this.dist/6371));
}
else if(this.numsides != 2){ // Spherical Polygon of n sides, theta is sum of internal angles: area = (theta-(n-2)*pi)*R^2
var latlon = destinationr(0,0,this.dist,pi);
var latlon2 = destinationr(0,0,this.dist,pi-2*pi/this.numsides);
var ang = 2*bearing(latlon[0],latlon[1],latlon2[0],latlon2[1]);
area = ((this.numsides*ang)-(this.numsides-2)*pi)*6371*6371;
circum = this.numsides*distance(latlon[0],latlon[1],latlon2[0],latlon2[1]);
}
}
Number.prototype.toRad = function() { // convert degrees to radians
return this * pi / 180;
}
Number.prototype.toDeg = function() { // convert radians to degrees
return this * 180 / pi;
}
Number.prototype.fixLon = function() { // keep longitude in range -180 to 180
lon = this;
while (lon < -180) {lon +=360;}
while (lon > 180) {lon -=360;}
return parseFloat(lon);
}
Function distance (lata,lona,latb,lonb) { // great circle distance (km)
return Math.acos(Math.sin(lata)*Math.sin(latb)+Math.cos(lata)*Math.cos(latb)*Math.cos(lonb-lona))*6371;
}
Function bearing(lata,lona,latb,lonb) { // initial great circle bearing (rad)
return Math.atan2(Math.sin(lonb-lona)*Math.cos(latb), Math.cos(lata)*Math.sin(latb)-Math.sin(lata)*Math.cos(latb)*Math.cos(lonb-lona))
}
Function destination(lata,lona,dist,brng) { // destination along great circle. returns values in degrees
var latb = Math.asin(Math.sin(lata)*Math.cos(dist/6371) + Math.cos(lata)*Math.sin(dist/6371)*Math.cos(brng));
var lonb = lona+Math.atan2(Math.sin(brng)*Math.sin(dist/6371)*Math.cos(lata), Math.cos(dist/6371)-Math.sin(lata)*Math.sin(latb));
return [180*latb/pi, 180*lonb/pi]
}
Function destinationr(lata,lona,dist,brng) { // destination along great circle. returns value in radians
var latb = Math.asin(Math.sin(lata)*Math.cos(dist/6371) + Math.cos(lata)*Math.sin(dist/6371)*Math.cos(brng));
var lonb = lona+Math.atan2(Math.sin(brng)*Math.sin(dist/6371)*Math.cos(lata), Math.cos(dist/6371)-Math.sin(lata)*Math.sin(latb));
return [latb, lonb]
}
</script>
<script type="text/javascript">
Google.load("earth", "1", {'other_params': 'sensor=false' });
Var ge = null;
Var pm = null;
Function init() {
google.earth.createInstance("map3d", initCB, failureCB);
}
Function initCB(object) {
ge = object;
ge.getWindow().setVisibility(true);
ge.getOptions().setStatusBarVisibility(true);
var navControl = ge.getNavigationControl();
navControl.setVisibility(ge.VISIBILITY_SHOW);
var dist=5;
var R = 6371;
var d = parseFloat(dist)/R; // d = angular distance covered on earth's surface
var lat1 = 42.32331619631114;
var lon1 = 2.598912324753657;
lat1 = lat1.toRad();
lon1 = lon1.toRad();
var bear=1;
bear = bear.toRad();
var lat2 = lat1 + d*Math.cos(bear);
var dLat = lat2-lat1;
var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1); // E-W line gives dPhi=0
var dLon = d*Math.sin(bear)/q;
// check for some daft bugger going past the pole
if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -(Math.PI-lat2);
lon2 = (lon1+dLon+3*Math.PI)%(2*Math.PI) - Math.PI;
/*
La segunda coordenada se obtiene restando el valor obtenido
En el codigo anterior a las coordenadas originales.
NO SE SI ES CORRECTO ESTO
*/
Pm = new Polygon(42.32331619631114,2.598912324753657,42.32331619631114-lat2,2.598912324753657-lon2,document.getElementById('polygonselect').value);
// Creamos la vista para que salga centrado
Var lookAt = ge.createLookAt('');
LookAt.setLatitude(42.32331619631114);
LookAt.setLongitude(2.598912324753657);
LookAt.setRange(400000.0);
Ge.getView().setAbstractView(lookAt);
}
Function failureCB(object) {
// alert('load failed');
}
</script>
<style type="text/css">
Select.s {font-size: 10px;}
Input.vs {font-size: 8px;}
Input.s {font-size: 9px;}
</style>
</head>
<body onload='init()' id='body'>
<div style='float:left; width:250px; height:550px; overflow:auto'>
<table style='font-size:small'>
<tr><td>Centre: </td><td><span id='centre'></span></td></tr>
<tr><td>Vertex: </td><td><span id='outer'></span></td></tr>
<tr><td>Radius: </td><td><span id='rad'></span></td></tr>
<tr><td>Bearing: </td><td><span id='bear'></span></td></tr>
<script type="text/javascript">pm.drawPolygon();</script>
<tr><td><form id="shape" action='javascript:void(0);'><p>
<select name="polygon" id="polygonselect" onchange='pm.numsides = this.value; pm.drawPolygon()' class="s">
<option value="100" class="vs">Circle</option>
<option value="2" class="vs">Line</option>
<option value="3" class="vs">Triangle</option>
<option value="4" class="vs">Square</option>
<option value="5" class="vs">Pentagon</option>
<option value="6" class="vs">Hexagon</option>
<option value="7" class="vs">Heptagon</option>
<option value="8" class="vs">Octagon</option>
<option value="9" class="vs">Nonagon</option>
<option value="10" class="vs">Decagon</option>
<option value="11" class="vs">Hendecagon</option>
<option value="12" class="vs">Dodecagon</option>
</select></p></form></td><td>
</table>
<hr />
</div>
<div id='map3d_container' style='border: 1px solid silver; height: 550px; margin-left:250px;'>
<div id='map3d' style='height: 100%;'></div>
</div>
</body>
</html>