Skip to content
Snippets Groups Projects
earth.html 8.44 KiB
Newer Older
  • Learn to ignore specific revisions
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    pixhawk's avatar
    pixhawk committed
      <head>
    
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <!-- <head> -->
    
    <!-- QGroundControl -->
    
    pixhawk's avatar
    pixhawk committed
        <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 *** -->
    
        <script type="text/javascript" src="https://getfirebug.com/firebug-lite-beta.js"></script>
    
        <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
        <script type="text/javascript">
    
    google.load("earth", "1", { 'language': 'en'});
    
    pixhawk's avatar
    pixhawk committed
    
    var ge = null;
    
    var initialized = false;
    var currAircraft = 220;
    
    var followEnabled = false;
    
    lm's avatar
    lm committed
    var lastAlt = 0;
    
    var currLat = 47.3769;
    var currLon = 8.549444;
    var currAlt = 470;
    
    var currFollowHeading = 0.0;
    
    
    var homeLat = 0;
    var homeLon = 0;
    var homeAlt = 0;
    var homeViewRange = 500;
    var homeLocation = null;
    var homeGroundLevel = 0;
    
    
    var currViewRange = 50.0;	///<< The current viewing range from this position (in meters)
    
    var currTilt = 40.0;		///<< The tilt angle (in degrees)
    var currFollowTilt = 40.0;
    var currView = null;
    
    
    var M_PI = 3.14159265;
    
    
    
    
    var planeOrient;
    var planeLoc;
    
    
    var aircraft = [];
    
    var aircraftLocations = [];
    var aircraftLastLocations = [];
    
    var attitudes = [];
    var locations = [];
    var trails = [];
    
    var trailPlacemarks = [];
    var trailsVisible = [];
    var trailColors = [];
    //var trailPlacemarks[id];
    
    
    // Aircraft class
    
    var planeColor = '6600ffff';
    
    
    
    function isInitialized()
    {
    	return initialized;
    }
    
    
    	google.earth.createInstance("map3d", initCallback, failureCallback);
    }
    
    
    pixhawk's avatar
    pixhawk committed
    function setFollowEnabled(enable)
    {
    	followEnabled = enable;
    }
    
    
    function setCurrAircraft(id)
    {
    	currAircraft = id;
    }
    
    
    function setGCSHome(lat, lon, alt)
    {
    	homeLat = lat;
    	homeLon = lon;
    	homeAlt = alt;
    	
    	
    	
    	var placemark = ge.createPlacemark(''); 
    	    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); 
    	    placemark.setStyleSelector(style);  
    
    	    // 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. 
    	    ge.getFeatures().appendChild(placemark); 
    
    	    homeGroundLevel = ge.getGlobe().getGroundAltitude(lat,lon);
    	    if (homeGroundLevel == 0)
    	    {
    		    homeGroundLevel = alt;
    	    }
    
    pixhawk's avatar
    pixhawk committed
    function createAircraft(id, type, color)
    
    	      planePlacemark = ge.createPlacemark('');
    
              planePlacemark.setName('aircraft');
              planeModel = ge.createModel('');
              ge.getFeatures().appendChild(planePlacemark);
              planeLoc = ge.createLocation('');
              planeModel.setLocation(planeLoc);
              planeLink = ge.createLink('');
              planeOrient = ge.createOrientation(''); 
              planeModel.setOrientation(planeOrient); 
    
              planeLink.setHref('http://qgroundcontrol.org/_media/users/models/multiplex-twinstar.dae');
              planeModel.setLink(planeLink);
              planeModel.setAltitudeMode (ge.ALTITUDE_ABSOLUTE); 
    
              planeLoc.setLatitude(currLat);
              planeLoc.setLongitude(currLon);
              planeLoc.setAltitude(currAlt);
    
              planePlacemark.setGeometry(planeModel);
              
    
    pixhawk's avatar
    pixhawk committed
              // Write into global structure
              aircraft[id] = planePlacemark;
              attitudes[id] = planeOrient;
    
              aircraftLocations[id] = planeLoc;
    
    		  aircraftLastLocations[id] = ge.createLocation('');
    
              createTrail(id, color);
              console.log(color);
    
    pixhawk's avatar
    pixhawk committed
    }
    
    function createTrail(id, color)
    {
    
              trailPlacemarks[id] = ge.createPlacemark('');
    
    pixhawk's avatar
    pixhawk committed
    // 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
    
    // Add LineString points
    //lineString.getCoordinates().pushLatLngAlt(48.754, -121.835, 700);
    
    // Create a style and set width and color of line
    
    trailPlacemarks[id].setStyleSelector(ge.createStyle(''));
    lineStyle = trailPlacemarks[id].getStyleSelector().getLineStyle();
    
    pixhawk's avatar
    pixhawk committed
    lineStyle.setWidth(5);
    
    trailColors[id] = color;
    lineStyle.getColor().set('00000000');  // aabbggrr format
    
    trailsVisible[id] = false;
    
    pixhawk's avatar
    pixhawk committed
    
    // Add the feature to Earth
    
    //ge.getFeatures().appendChild(trailPlacemarks[id]);
    
    lm's avatar
    lm committed
    function clearTrail(id)
    
    {
    	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
    function addTrailPosition(id, lat, lon, alt)
    {
    
    	trails[id].setExtrude(false);
    
        trails[id].setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
    
    	trails[id].getCoordinates().pushLatLngAlt(lat, lon, alt);
    
        // 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
        //lineStyle.getColor().set(color);  // aabbggrr format
    
        // Add the feature to Earth
        if (trailsVisible[id] == true) ge.getFeatures().replaceChild(trailPlacemarks[id], trailPlacemarks[id]);
    
    function initCallback(object)
    {
    
        ge = object;
        ge.getWindow().setVisibility(true);
        ge.getOptions().setStatusBarVisibility(true); 
        ge.getOptions().setScaleLegendVisibility(true);
    
    pixhawk's avatar
    pixhawk committed
        //ge.getOptions().setFlyToSpeed(5.0); 
        ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT); 
    
        ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    
        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);
    
    }
    
    function setAircraftPositionAttitude(id, lat, lon, alt, roll, pitch, yaw)
    {
    	if (id == currAircraft)
    	{
    
    lm's avatar
    lm committed
    		if (lastLat == 0)
    		{
    			lastLat = currLat;
    
    pixhawk's avatar
    pixhawk committed
    			lastLon = currLon;
    
    lm's avatar
    lm committed
    		}
    
    		currLat = lat;
    		currLon = lon;
    		currAlt = alt;
    
    pixhawk's avatar
    pixhawk committed
    		// Interpolate between t-1 and t and set new states
    		lastLat = lastLat*0.8+currLat*0.2;
    		lastLon = lastLon*0.8+currLon*0.2;
    		lastAlt = lastAlt*0.8+currAlt*0.2;
    
    		//currFollowHeading = ((yaw/M_PI)+1.0)*360.0;
    
    	// FIXME Currently invalid conversion from right-handed z-down to z-up frame
    
    	planeOrient.setRoll(((roll/M_PI))*180.0+180.0);
    	planeOrient.setTilt(((pitch/M_PI))*180.0+180.0);
    
    	planeOrient.setHeading(((yaw/M_PI))*180.0-90.0);
    
    pixhawk's avatar
    pixhawk committed
    	
    
    	currFollowHeading = ((yaw/M_PI))*180.0;
    
    pixhawk's avatar
    pixhawk committed
        planeLoc.setLatitude(lastLat);
        planeLoc.setLongitude(lastLon);
        planeLoc.setAltitude(lastAlt);
    
    pixhawk's avatar
    pixhawk committed
        
        if (followEnabled) updateFollowAircraft();
    
    function enableDaylight(enabled)
    {
    	if(enabled)
    	{
    		ge.getSun().setVisibility(true);
    	}
    	else
    	{
    		ge.getSun().setVisibility(false);
    	}
    }
    		
    
    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);
    
    
    function setCurrentAircraft(id)
    {
    	currAircraft = id;
    }
    
    function updateFollowAircraft()
    {
    	currView = ge.getView().copyAsLookAt(ge.ALTITUDE_ABSOLUTE);
    
    	currView.setLatitude(lastLat);
    	currView.setLongitude(lastLon);
    	currView.setAltitude(lastAlt);
    
    
    	currView.setRange(currViewRange);
    	currView.setTilt(currFollowTilt);
    
    	currView.setHeading(currFollowHeading);
    
    	ge.getView().setAbstractView(currView);
    }
    
    function failureCallback(object)
    {
    }
    </script>
    
    
    
    pixhawk's avatar
    pixhawk committed
        <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>
      </body>
    </html>