earth.html 22.3 KB
Newer Older
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 3

<html>
pixhawk's avatar
pixhawk committed
4
  <head>
5 6
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <!-- <head> -->
7
<!-- QGroundControl -->
pixhawk's avatar
pixhawk committed
8 9
    <title>QGroundControl Google Earth View</title>
    <!-- *** Replace the key below below with your own API key, available at http://code.google.com/apis/maps/signup.html *** -->
10
    <script type="text/javascript" src="https://getfirebug.com/firebug-lite-beta.js"></script>
11
    <script type="text/javascript" src="https://www.google.com/jsapi?key=ABQIAAAA5Q6wxQ6lxKS8haLVdUJaqhSjosg_0jiTTs2iXtkDVG0n0If1mBRHzhWw5VqBZX-j4NuzoVpU-UaHVg"></script>
12
    <script type="text/javascript">
13
google.load("earth", "1", { 'language': 'en'});
pixhawk's avatar
pixhawk committed
14
var ge = null;
15
var initialized = false;
lm's avatar
lm committed
16
var currAircraft = 0;
17
var followEnabled = false;
lm's avatar
lm committed
18
var lineAltitudeOffset = 0.5; ///< 0.5 m higher than waypoint, prevents the line from entering the ground
19

LM's avatar
LM committed
20 21 22
var lastLat = [];
var lastLon = [];
var lastAlt = [];
23 24 25
var currLat = 47.3769;
var currLon = 8.549444;
var currAlt = 470;
26 27 28 29 30

var currentCameraLatitude = 0;
var currentCameraLongitude = 0;
var currentCameraAltitude = 0;

31
var currFollowHeading = 0.0;
32 33 34 35

var homeLat = 0;
var homeLon = 0;
var homeAlt = 0;
36
var homeViewRange = 800;
37 38 39
var homeLocation = null;
var homeGroundLevel = 0;

40
var currViewRange = 50.0;	///<< The current viewing range from this position (in meters)
41 42 43
var currTilt = 40.0;		///<< The tilt angle (in degrees)
var currFollowTilt = 40.0;
var currView = null;
44
var distanceMode = 0;
45

46
var viewMode = 0;
47
var lastTilt = currTilt;
48 49 50
var lastRoll = 0;
var lastHeading = 0;

51 52
var M_PI = 3.14159265;

53 54 55 56 57


var planeOrient;
var planeLoc;

58
var aircraft = [];
59 60
var aircraftLocations = [];
var aircraftLastLocations = [];
LM's avatar
LM committed
61 62 63
var aircraftScaleFactors = [];
var aircraftLinks = [];
var aircraftModels = [];
64 65 66
var attitudes = [];
var locations = [];
var trails = [];
67 68 69
var trailPlacemarks = [];
var trailsVisible = [];
var trailColors = [];
pixhawk's avatar
pixhawk committed
70
var waypoints = [];
LM's avatar
LM committed
71 72 73 74

var waypointLines = [];
var waypointLinePlacemarks = [];
var waypointLineColors = [];
pixhawk's avatar
pixhawk committed
75
//var waypointLines = [];
76
//var trailPlacemarks[id];
77
var lineStyle;
78 79

// Aircraft class
80
var planeColor = '6600ffff';
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
// Enable / disable dragging
var draggingAllowed = true;

// Waypoint interaction flags
var dragInfo = null;

var dragWaypointIndex = "";
var dragWaypointLatitude = 0;
var dragWaypointLongitude = 0;
var dragWaypointAltitude = 0;
var dragWaypointPending = false;

// Waypoint creation flags
var newWaypointLatitude = 0;
var newWaypointLongitude = 0;
var newWaypointAltitude = 0;
var newWaypointPending = false;

100 101
var clickMode = 0;

102 103
var homePlacemark = null;

104 105 106 107 108

// Data / heightmap
var heightMapPlacemark = null;
var heightMapModel = null;

109 110 111 112 113 114 115 116 117 118
function getGlobal(variable)
{
	return variable;
}

function getDraggingAllowed()
{
	return draggingAllowed;
}

119 120 121 122 123
function setDistanceMode(mode)
{
	distanceMode = mode;
}

124 125
function setDraggingAllowed(allowed)
{
126
	draggingAllowed = allowed;
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
function sampleCurrentPosition()
{
    var thisView = ge.getView().copyAsLookAt(ge.ALTITUDE_ABSOLUTE);
	currentCameraLatitude = thisView.getLatitude();
	currentCameraLongitude = thisView.getLongitude();
	currentCameraGroundAltitude = ge.getGlobe().getGroundAltitude(currentCameraLatitude, currentCameraLongitude);
}

function enableSetHomeMode()
{
	clickMode = 1;
}

function setLookAtLatLon(lat, lon)
{
	// Keep the current altitude above ground, just move the position
	currView = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	currView.setLatitude(lat);
	currView.setLongitude(lon);
	ge.getView().setAbstractView(currView);
}

151 152 153
function setNewWaypointPending(pending)
{
	newWaypointPending = pending;
154
	document.getElementById('JScript_newWaypointPending').setAttribute('value',pending);
155 156 157 158 159
}

function setDragWaypointPending(pending)
{
	dragWaypointPending = pending;
160
	document.getElementById('JScript_dragWaypointPending').setAttribute('value',pending);
161
}
162 163 164 165 166 167

function isInitialized()
{
	return initialized;
}

pixhawk's avatar
pixhawk committed
168

169 170
function init()
{
171 172 173
	google.earth.createInstance("map3d", initCallback, failureCallback);
}

pixhawk's avatar
pixhawk committed
174 175 176 177 178
function setFollowEnabled(enable)
{
	followEnabled = enable;
}

179 180 181 182 183
function enableEventListener()
{
// listen for mousedown on the window (look specifically for point placemarks)
google.earth.addEventListener(ge.getWindow(), 'mousedown', function(event)
{
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
if (clickMode == 1)
    {
    	// Set home mode
    	dragWaypointIndex = 'HOME';
    	document.getElementById('JScript_dragWaypointIndex').setAttribute('value',dragWaypointIndex);
		dragWaypointLatitude = event.getLatitude();
		dragWaypointLongitude = event.getLongitude();
		dragWaypointAltitude = ge.getGlobe().getGroundAltitude(dragWaypointLatitude, dragWaypointLongitude);
		dragWaypointPending = true;
		document.getElementById('JScript_dragWaypointLatitude').setAttribute('value',dragWaypointLatitude);
	   document.getElementById('JScript_dragWaypointLongitude').setAttribute('value',dragWaypointLongitude);
	   document.getElementById('JScript_dragWaypointAltitude').setAttribute('value',dragWaypointAltitude);
	   document.getElementById('JScript_dragWaypointPending').setAttribute('value',true);
	   setGCSHome(dragWaypointLatitude, dragWaypointLongitude, dragWaypointAltitude);
}
    
200 201 202
  if (event.getTarget().getType() == 'KmlPlacemark' &&
      event.getTarget().getGeometry().getType() == 'KmlPoint') {
    var placemark = event.getTarget();
203 204 205
    event.preventDefault();
      if (draggingAllowed)
  {
206 207
  	if (clickMode == 0)
  	{
208 209 210 211
    dragInfo = {
      placemark: event.getTarget(),
      dragged: false
    };
212
    }
213 214 215 216 217 218 219
  }
  }
});

// listen for mousemove on the globe
google.earth.addEventListener(ge.getGlobe(), 'mousemove', function(event)
{
220
  if (draggingAllowed && (clickMode == 0))
221
  {
lm's avatar
lm committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  	if (dragInfo)
  	{
    	event.preventDefault();
    	var point = dragInfo.placemark.getGeometry();
    	point.setLatitude(event.getLatitude());
    	point.setLongitude(event.getLongitude());
    	dragInfo.dragged = true;
    	dragWaypointIndex = dragInfo.placemark.getDescription();
		document.getElementById('JScript_dragWaypointIndex').setAttribute('value',dragWaypointIndex);
		dragWaypointLatitude = event.getLatitude();
		dragWaypointLongitude = event.getLongitude();
		dragWaypointAltitude = point.getAltitude();
		dragWaypointPending = true;
		document.getElementById('JScript_dragWaypointLatitude').setAttribute('value',dragWaypointLatitude);
	    document.getElementById('JScript_dragWaypointLongitude').setAttribute('value',dragWaypointLongitude);
	    document.getElementById('JScript_dragWaypointAltitude').setAttribute('value',dragWaypointAltitude);
	    document.getElementById('JScript_dragWaypointPending').setAttribute('value',true);
  	}
240 241 242 243 244 245
  }
});

// listen for mouseup on the window
google.earth.addEventListener(ge.getWindow(), 'mouseup', function(event)
{
246
  if (draggingAllowed && (clickMode == 0))
247 248 249 250 251 252 253 254
  {
  if (dragInfo) {
    if (dragInfo.dragged)
    {
      	// if the placemark was dragged, prevent balloons from popping up
      	event.preventDefault();
      	// Get drag end location
      	dragWaypointIndex = dragInfo.placemark.getDescription();
255
		document.getElementById('JScript_dragWaypointIndex').setAttribute('value',dragWaypointIndex);
256 257 258 259 260
      	var point = dragInfo.placemark.getGeometry();
      	dragWaypointLatitude = event.getLatitude();
		dragWaypointLongitude = event.getLongitude();
		dragWaypointAltitude = point.getAltitude();
    	dragWaypointPending = true;
261 262 263 264
		document.getElementById('JScript_dragWaypointLatitude').setAttribute('value',dragWaypointLatitude);
	   document.getElementById('JScript_dragWaypointLongitude').setAttribute('value',dragWaypointLongitude);
	   document.getElementById('JScript_dragWaypointAltitude').setAttribute('value',dragWaypointAltitude);
	   document.getElementById('JScript_dragWaypointPending').setAttribute('value',true);
265 266 267 268 269
    }
    
    dragInfo = null;
  }
  }
270
      clickMode = 0;
271 272 273 274 275 276 277 278 279 280 281 282
});

// Listen for wp creation request on the globe

google.earth.addEventListener(ge.getGlobe(), 'dblclick', function(event){
	if (draggingAllowed)
	{
       event.preventDefault();
       newWaypointLatitude = event.getLatitude();
       newWaypointLongitude = event.getLongitude();
       newWaypointAltitude = ge.getGlobe().getGroundAltitude(newWaypointLatitude, newWaypointLongitude);
       newWaypointPending = true;
283 284 285 286
	   document.getElementById('JScript_newWaypointLatitude').setAttribute('value',newWaypointLatitude);
	   document.getElementById('JScript_newWaypointLongitude').setAttribute('value',newWaypointLongitude);
	   document.getElementById('JScript_newWaypointAltitude').setAttribute('value',newWaypointAltitude);
	   document.getElementById('JScript_newWaypointPending').setAttribute('value',true);
287 288 289 290
    }
}); 
}

291 292


293 294 295 296
function setCurrAircraft(id)
{
	currAircraft = id;
}
297 298 299

function setGCSHome(lat, lon, alt)
{
300 301 302
	// Only update if position actually changed
	if (lat != homeLat || lon != homeLon || alt != homeAlt)
	{
303 304 305 306
	homeLat = lat;
	homeLon = lon;
	homeAlt = alt;
	
307 308
	if (homePlacemark == null)
	{
lm's avatar
lm committed
309
		var placemark = ge.createPlacemark(''); 
310 311 312 313 314
	    var icon = ge.createIcon(''); 
	    icon.setHref('http://google-maps-icons.googlecode.com/files/blackH.png'); 
	    var style = ge.createStyle(''); 
	    style.getIconStyle().setIcon(icon); 
	    //style.getIconStyle().setScale(0.5); 
315 316
	    placemark.setStyleSelector(style);
	    placemark.setDescription('HOME');
317 318 319 320 321 322 323 324 325

	    // Set the placemark's location.   
	    homeLocation = ge.createPoint(''); 
	    homeLocation.setLatitude(lat); 
	    homeLocation.setLongitude(lon);
	    homeLocation.setAltitude(alt); 
	    placemark.setGeometry(homeLocation);  

	    // Add the placemark to Earth. 
326 327 328 329 330 331
	    ge.getFeatures().appendChild(placemark);
	    
	    homePlacemark = placemark;
	    }
	    else
	    {
332 333 334 335 336 337 338 339 340
	    	var location = ge.createPoint('');
	    	if (location.getLatitude() != lat || location.getLongitude() != lon || location.getAltitude() != alt)
	    	{
	    		location.setLatitude(lat); 
	    		location.setLongitude(lon);
	    		location.setAltitude(alt); 
	    		homePlacemark.setGeometry(location);
	    		homePlacemark.setDescription('HOME');
	    	}
341
	    }
342 343 344 345 346 347

	    homeGroundLevel = ge.getGlobe().getGroundAltitude(lat,lon);
	    if (homeGroundLevel == 0)
	    {
		    homeGroundLevel = alt;
	    }
348
	}
pixhawk's avatar
pixhawk committed
349 350
}

pixhawk's avatar
pixhawk committed
351 352 353 354 355
function updateWaypointListLength(id, len)
{
	// Delete any non-needed waypoints
	if (waypoints.length > len)
	{
356 357
		var initialLength = waypoints.length;
		for (var i=len; i<initialLength; i++)
pixhawk's avatar
pixhawk committed
358 359 360
		{
			var placemark = waypoints.pop();
		    ge.getFeatures().removeChild(placemark);
361
		    waypointLines[id].getCoordinates().pop();
pixhawk's avatar
pixhawk committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		}
	}
}

function updateWaypoint(id, index, lat, lon, alt, action)
{
	// Check if waypoint exists
	if (waypoints.length > index)
	{
		// Waypoint exists
		// Set the placemark's location.   
	    var location = ge.createPoint(''); 
	    location.setLatitude(lat); 
	    location.setLongitude(lon);
	    location.setAltitude(alt); 
lm's avatar
lm committed
377
	    waypoints[index].setGeometry(location);
378
	    waypoints[index].setDescription(index+"");
LM's avatar
LM committed
379
	    
lm's avatar
lm committed
380 381
	    // Set the WP line location
	    waypointLines[id].getCoordinates().setLatLngAlt(index, lat, lon, alt);
pixhawk's avatar
pixhawk committed
382 383 384 385 386 387 388 389 390
	}
	else
	{
		// Waypoint does not exist yet
		var placemark = ge.createPlacemark(''); 
	    var icon = ge.createIcon('');
	    var numberstring = index;
	    if (index < 10) numberstring = '0' + numberstring
	    icon.setHref('http://google-maps-icons.googlecode.com/files/red' + numberstring +'.png'); 
lm's avatar
lm committed
391
	    var style = ge.createStyle('');
392
	    //console.log('WP ICON created:' + 'http://google-maps-icons.googlecode.com/files/red' + numberstring +'.png');
pixhawk's avatar
pixhawk committed
393 394
	    style.getIconStyle().setIcon(icon); 
	    //style.getIconStyle().setScale(0.5); 
395 396
	    placemark.setStyleSelector(style);
	    placemark.setDescription(index+"");
pixhawk's avatar
pixhawk committed
397 398 399 400 401

	    // Set the placemark's location.   
	    var location = ge.createPoint(''); 
	    location.setLatitude(lat); 
	    location.setLongitude(lon);
lm's avatar
lm committed
402
	    location.setAltitude(alt+lineAltitudeOffset); 
pixhawk's avatar
pixhawk committed
403 404 405 406 407
	    placemark.setGeometry(location);  

	    // Add the placemark to Earth. 
	    ge.getFeatures().appendChild(placemark); 
		waypoints[index] = placemark;
lm's avatar
lm committed
408
        
LM's avatar
LM committed
409 410
        // Add LineString points
	    waypointLines[id].getCoordinates().pushLatLngAlt(lat, lon, alt);
lm's avatar
lm committed
411
	}
pixhawk's avatar
pixhawk committed
412 413
}

pixhawk's avatar
pixhawk committed
414
function createAircraft(id, type, color)
415
{
LM's avatar
LM committed
416 417 418 419 420 421 422 423 424
	aircraft[id] = ge.createPlacemark('');
	aircraft[id].setName('aircraft');
	aircraftModels[id] = ge.createModel('');
	ge.getFeatures().appendChild(aircraft[id]);
	aircraftLocations[id] = ge.createLocation('');
	aircraftModels[id].setLocation(aircraftLocations[id]);
	aircraftLinks[id] = ge.createLink('');
	attitudes[id] = ge.createOrientation(''); 
	aircraftModels[id].setOrientation(attitudes[id]);
425
	
LM's avatar
LM committed
426
	aircraftScaleFactors[id] = 1.0;
lm's avatar
lm committed
427

428
	//planeLink.setHref('http://www.asl.ethz.ch/people/rudink/senseSoarDummy.dae'); 
LM's avatar
LM committed
429 430
	aircraftLinks[id].setHref('http://qgroundcontrol.org/_media/users/models/sfly-hex.dae');
	aircraftScaleFactors[id] = 1.0/1000.0;
431
	//planeLink.setHref('http://qgroundcontrol.org/_media/users/models/ascent-park-glider.dae');
LM's avatar
LM committed
432 433 434 435 436
	aircraftModels[id].setLink(aircraftLinks[id]);
	var scale = aircraftModels[id].getScale();
	scale.set(scale.getX()*aircraftScaleFactors[id], scale.getY()*aircraftScaleFactors[id], scale.getZ()*aircraftScaleFactors[id])
	aircraftModels[id].setScale(scale);
	aircraftModels[id].setAltitudeMode (ge.ALTITUDE_ABSOLUTE); 
lm's avatar
lm committed
437

LM's avatar
LM committed
438 439 440
	aircraftLocations[id].setLatitude(currLat);
	aircraftLocations[id].setLongitude(currLon);
	aircraftLocations[id].setAltitude(currAlt);
lm's avatar
lm committed
441

LM's avatar
LM committed
442
	aircraft[id].setGeometry(aircraftModels[id]);
443
          
lm's avatar
lm committed
444
	// Write into global structure
LM's avatar
LM committed
445 446 447 448
	aircraftLastLocations[id] = aircraftLocations[id];
	lastLat[id] = 0;
	lastLon[id] = 0;
	lastAlt[id] = 0;
pixhawk's avatar
pixhawk committed
449
          
lm's avatar
lm committed
450 451
	createTrail(id, color);
	createWaypointLine(id, color);
pixhawk's avatar
pixhawk committed
452 453 454 455
}

function createTrail(id, color)
{
lm's avatar
lm committed
456 457 458 459 460 461 462 463
	trailPlacemarks[id] = ge.createPlacemark('');
	// Create the placemark
	// Create the LineString; set it to extend down to the ground
	// and set the altitude mode
	trails[id] = ge.createLineString('');
	trailPlacemarks[id].setGeometry(trails[id]);
	trails[id].setExtrude(false);
	trails[id].setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
pixhawk's avatar
pixhawk committed
464

lm's avatar
lm committed
465 466 467 468 469 470
	// Create a style and set width and color of line
	trailPlacemarks[id].setStyleSelector(ge.createStyle(''));
	lineStyle = trailPlacemarks[id].getStyleSelector().getLineStyle();
	lineStyle.setWidth(5);
	trailColors[id] = color;
	lineStyle.getColor().set('00000000');  // aabbggrr format
471

lm's avatar
lm committed
472
	trailsVisible[id] = false;
pixhawk's avatar
pixhawk committed
473 474
}

LM's avatar
LM committed
475 476
function createWaypointLine(id, color)
{
lm's avatar
lm committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	// Create the placemark
	waypointLinePlacemarks[id] = ge.createPlacemark('');
	// Create the LineString; set it to extend down to the ground
	// and set the altitude mode
	waypointLines[id] = ge.createLineString('');
	waypointLinePlacemarks[id].setGeometry(waypointLines[id]);
	waypointLines[id].setExtrude(false);
	waypointLines[id].setAltitudeMode(ge.ALTITUDE_ABSOLUTE);

	// Add LineString points
	//lineString.getCoordinates().pushLatLngAlt(48.754, -121.835, 700);

	// Create a style and set width and color of line
	waypointLinePlacemarks[id].setStyleSelector(ge.createStyle(''));
	lineStyle = waypointLinePlacemarks[id].getStyleSelector().getLineStyle();
	lineStyle.setWidth(15);
	waypointLineColors[id] = color;
	lineStyle.getColor().set(color);  // aabbggrr format
LM's avatar
LM committed
495

lm's avatar
lm committed
496
        
LM's avatar
LM committed
497

lm's avatar
lm committed
498 499 500 501 502 503
        // Create a style and set width and color of line
        //waypointLinePlacemarks[id].setStyleSelector(ge.createStyle(''));
        //lineStyle = waypointLinePlacemarks[id].getStyleSelector().getLineStyle();
        //lineStyle.setWidth(15);
        //lineStyle.getColor().set(waypointLineColors[id]);  // aabbggrr format
        //lineStyle.getColor().set(color);  // aabbggrr format
LM's avatar
LM committed
504

lm's avatar
lm committed
505 506
	// Add the feature to Earth
	ge.getFeatures().appendChild(waypointLinePlacemarks[id]);
LM's avatar
LM committed
507 508
}

lm's avatar
lm committed
509
function clearTrail(id)
510 511 512 513 514 515 516 517 518 519 520 521
{
	ge.getFeatures().removeChild(trailPlacemarks[id]);
	trailPlacemarks[id] = null;
	var isVisible = trailsVisible[id];
	createTrail(id, trailColors[id]);
	if (isVisible)
	{
		showTrail(id);
	}
}

function hideTrail(id)
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
{
	trailsVisible[id] = false;
	ge.getFeatures().removeChild(trailPlacemarks[id]);
}

function showTrail(id)
{
	ge.getFeatures().appendChild(trailPlacemarks[id]);
	trailsVisible[id] = true;
}

function setViewRange(dist)
{
	currViewRange = dist;
}

pixhawk's avatar
pixhawk committed
538 539
function addTrailPosition(id, lat, lon, alt)
{
lm's avatar
lm committed
540 541
	//trails[id].setExtrude(false);
    //trails[id].setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
542

543
    // Add LineString points
544
	trails[id].getCoordinates().pushLatLngAlt(lat, lon, alt);
545

546 547 548 549 550
    // Create a style and set width and color of line
    trailPlacemarks[id].setStyleSelector(ge.createStyle(''));
    lineStyle = trailPlacemarks[id].getStyleSelector().getLineStyle();
    lineStyle.setWidth(5);
    lineStyle.getColor().set(trailColors[id]);  // aabbggrr format
pixhawk's avatar
pixhawk committed
551 552
}

553 554
function initCallback(object)
{
555 556 557 558
    ge = object;
    ge.getWindow().setVisibility(true);
    ge.getOptions().setStatusBarVisibility(true); 
    ge.getOptions().setScaleLegendVisibility(true);
pixhawk's avatar
pixhawk committed
559 560
    //ge.getOptions().setFlyToSpeed(5.0); 
    ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT); 
561
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
pixhawk's avatar
pixhawk committed
562
        
563 564 565 566
    ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_TREES, true);
567
    
LM's avatar
LM committed
568
    
569 570 571 572
	
	// Load heightmap
	// http://www.inf.ethz.ch/personal/lomeier/data/untex-environment.dae
	
LM's avatar
LM committed
573
	/*heightMapPlacemark = ge.createPlacemark('');
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	heightMapPlacemark.setName('aircraft');
	heightMapModel = ge.createModel('');
	ge.getFeatures().appendChild(heightMapPlacemark);
	planeLoc = ge.createLocation('');
	heightMapModel.setLocation(planeLoc);
	planeLink = ge.createLink('');
	planeOrient = ge.createOrientation(''); 
	heightMapModel.setOrientation(planeOrient); 

	planeLink.setHref('http://www.inf.ethz.ch/personal/lomeier/data/untex-environment.dae'); 
	heightMapModel.setLink(planeLink);
	var scale = heightMapModel.getScale();
	var factor = 1.0;//1.0/1000.0;
	scale.set(scale.getX()*factor, scale.getY()*factor, scale.getZ()*factor)
	heightMapModel.setScale(scale);
	heightMapModel.setAltitudeMode (ge.ALTITUDE_ABSOLUTE); 

	planeLoc.setLatitude(currLat);
	planeLoc.setLongitude(currLon);
	planeLoc.setAltitude(currAlt);

LM's avatar
LM committed
595 596 597
	heightMapPlacemark.setGeometry(heightMapModel);*/
	enableEventListener();
    document.getElementById('JScript_initialized').setAttribute('value','true');
598
    initialized = true;
599 600 601 602
}

function setAircraftPositionAttitude(id, lat, lon, alt, roll, pitch, yaw)
{
LM's avatar
LM committed
603 604 605 606 607 608
	if (lastLat[id] == 0)
	{
		lastLat[id] = lat;
		lastLon[id] = lon;
	}
	
609 610
	if (id == currAircraft)
	{
LM's avatar
LM committed
611
		currFollowHeading = currFollowHeading*0.95+0.05*(((yaw/M_PI))*180.0);
612 613
		currLat = lat;
		currLon = lon;
614 615 616
		var trueGroundAlt = ge.getGlobe().getGroundAltitude(lat, lon);
		if (trueGroundAlt < alt)
		{
LM's avatar
LM committed
617
		  	currAlt = alt;
618 619 620
	    }
	    else
	    {
LM's avatar
LM committed
621
	     	currAlt = trueGroundAlt+0.1;
622
	    }
LM's avatar
LM committed
623 624 625 626 627 628
	}
	    
	// Interpolate between t-1 and t and set new states
	//lastLat[id] = lastLat[id]*0.5+lat*0.5;
	//lastLon[id] = lastLon[id]*0.5+lon*0.5;
	//lastAlt[id] = lastAlt[id]*0.5+alt*0.5;
629
	
LM's avatar
LM committed
630 631 632
	lastLat[id] = lastLat[id]*0.5+lat*0.5;
	lastLon[id] = lastLon[id]*0.5+lon*0.5;
	lastAlt[id] = lastAlt[id]*0.5+alt*0.5;
pixhawk's avatar
pixhawk committed
633
	
LM's avatar
LM committed
634 635 636 637
	attitudes[id].setRoll(+((roll/M_PI))*180.0);
	attitudes[id].setTilt(-((pitch/M_PI))*180.0);
	attitudes[id].setHeading(((yaw/M_PI))*180.0-90.0);
	aircraftModels[id].setOrientation(attitudes[id]);
638
    
LM's avatar
LM committed
639 640 641 642
    aircraftLocations[id].setLatitude(lastLat[id]);
    aircraftLocations[id].setLongitude(lastLon[id]);
    aircraftLocations[id].setAltitude(lastAlt[id]);
    aircraftModels[id].setLocation(aircraftLocations[id]);
pixhawk's avatar
pixhawk committed
643
    
LM's avatar
LM committed
644
    if (followEnabled && id == currAircraft) updateFollowAircraft();
pixhawk's avatar
pixhawk committed
645 646
}

647 648 649 650 651 652 653 654 655 656 657
function enableDaylight(enabled)
{
	if(enabled)
	{
		ge.getSun().setVisibility(true);
	}
	else
	{
		ge.getSun().setVisibility(false);
	}
}
658 659 660 661 662

function enableAtmosphere(enabled)
{
	ge.getOptions().setAtmosphereVisibility(enabled);
}
663
		
664 665 666 667 668 669 670 671 672
function goHome()
{
	var currView = ge.getView().copyAsLookAt(ge.ALTITUDE_ABSOLUTE);
	currView.setLatitude(homeLat);
	currView.setLongitude(homeLon);
	currView.setAltitude(homeAlt);
	currView.setRange(homeViewRange);
	currView.setTilt(currTilt);
	ge.getView().setAbstractView(currView);
pixhawk's avatar
pixhawk committed
673
}
674 675 676 677 678 679

function setCurrentAircraft(id)
{
	currAircraft = id;
}

680 681
/** @brief Set the current view mode
 *
682
 * @param mode 0: side map view, 1: top/north map view, 2: fixed chase cam, 3: free chase cam
683 684 685
 */
function setViewMode(mode)
{
686
	var currView = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
687 688 689
	
	if (mode == 0)
	{
690 691
		currView.setTilt(lastTilt);
		currView.setHeading(lastHeading);
692
	}
lm's avatar
lm committed
693
	
694
	if (mode == 1 && viewMode != mode)
695 696 697 698 699 700 701
	{
		lastTilt = currView.getTilt();
		lastHeading = currView.getHeading();
		currView.setTilt(0);
		currView.setHeading(0);
	}
	
702 703
	viewMode = mode;
	
704
	ge.getView().setAbstractView(currView);
705 706
}

707 708 709
function updateFollowAircraft()
{
	currView = ge.getView().copyAsLookAt(ge.ALTITUDE_ABSOLUTE);
LM's avatar
LM committed
710 711
	currView.setLatitude(lastLat[currAircraft]);
	currView.setLongitude(lastLon[currAircraft]);
712 713 714
	
	if (distanceMode == 1)
	{
LM's avatar
LM committed
715
		var groundAltitude = ge.getGlobe().getGroundAltitude(lastLat[currAircraft], lastLon[currAircraft]);
716 717 718
		currView.setAltitude(groundAltitude);
	}
	
LM's avatar
LM committed
719
	if (distanceMode == 0) currView.setAltitude(lastAlt[currAircraft]);
720

721
	currView.setRange(currViewRange);
722 723 724
	
	if (viewMode != 3) // VIEW_MODE_CHASE_FREE
	{
725 726
		currView.setTilt(currFollowTilt);
		currView.setHeading(currFollowHeading);
727
	}
728
	ge.getView().setAbstractView(currView);
729 730 731 732 733 734 735 736
}

function failureCallback(object)
{
}
</script>


pixhawk's avatar
pixhawk committed
737 738 739 740 741 742 743 744 745 746 747 748
    <style type="text/css">
    html, body {
      margin: 0;
      width: 100%;
      height: 100%;
    }
    </style>
  </head>
  <body onload='init()' id='body'>
    <center>
      <div id='map3d' style='margin: 0; spacing: 0; height: 100%; width: 100%'></div>
    </center>
749 750 751 752 753 754 755 756 757 758
	<input type="hidden" id="JScript_initialized" value="false" />
    <input type="hidden" id="JScript_dragWaypointIndex" value="0" />
	<input type="hidden" id="JScript_dragWaypointLatitude" value="0" />
	<input type="hidden" id="JScript_dragWaypointLongitude" value="0" />
	<input type="hidden" id="JScript_dragWaypointAltitude" value="0" />
	<input type="hidden" id="JScript_dragWaypointPending" value="false" />
	<input type="hidden" id="JScript_newWaypointLatitude" value="0" />
	<input type="hidden" id="JScript_newWaypointLongitude" value="0" />
	<input type="hidden" id="JScript_newWaypointAltitude" value="0" />
	<input type="hidden" id="JScript_newWaypointPending" value="false" />
759 760 761
	<input type="hidden" id="JScript_currentCameraLatitude" value="0" />
	<input type="hidden" id="JScript_currentCameraLongitude" value="0" />
	<input type="hidden" id="JScript_currentCameraGroundAltitude" value="0" />
pixhawk's avatar
pixhawk committed
762 763
  </body>
</html>