Virtual Earth 3D Flight Simulator – part 3

Another class called Airport can be made to specify an Airport location. This class will have a method called FlyToHere that will cause the plane to fly to runway of the selected airport. This class should look like this:

using System;
using Microsoft.MapPoint;
using Microsoft.MapPoint.Rendering3D;
using Microsoft.MapPoint.Rendering3D.Cameras;
using Microsoft.MapPoint.Geometry.VectorMath;

namespace SimpleFlightSimulator
{
    public class Airport
    {
        private LatLonAlt _latlong;
        private RollPitchYaw _rpy;

        public Airport(double latitude, double longitude, double altitude, double pitch, double heading)
        {
            this._latlong = LatLonAlt.CreateUsingDegrees(latitude, longitude, altitude);
            this._rpy = new RollPitchYaw(0, pitch, heading);
        }

        public void FlyToHere(Host host)
        {
            host.Navigation.FlyTo(this._latlong, this._rpy.Pitch, this._rpy.Yaw);
        }
    }
}

The next step is to create an HTML page to load Virtual Earth into. A reference to the virtual Earth map control will need to be made. In a script tag you will need to create four functions; OnPageLoad, On3DPlugInLoaded, FlyToAirport, and ChangeCockpit. The OnPageLoad function will create and load a Virtual Earth map. It will then load in the new plug-in you created. The On3DPlugInLoaded function will activate the plug-in that you created. The FlyToAirport function will allow you to specify an airport to fly to from the client side. The ChangeCockpit method will allow you to change the cockpit overlay from the client side. The script section of your HTML page should look like this:

<title>Simple Flight Simulator</title>
<script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2" type="text/javascript"></script>
<script type="text/javascript">
var map;
var objectPlugInGuid;
var control3D;
var sampleName = "SimpleFlightSimulator";
var isLoaded = false;

function OnPageLoad() {
    map = new VEMap(‘myMap’);
    map.LoadMap(null, 2, VEMapStyle.Hybrid, false, VEMapMode.Mode3D);

    map.HideDashboard();
    map.HideScalebar();
    control3D = map.vemapcontrol.Get3DControl();
    control3D.AttachEvent("OnPlugInLoaded", "On3DPlugInLoaded");
    control3D.LoadPlugInDll("C:\\Users\\rbrundritt\\Documents\\VE3D\\SimpleFlightSimulator\\SimpleFlightSimulator\\bin\\Debug\\SimpleFlightSimulator.dll"); // local path to where your dll lives
}

function On3DPlugInLoaded(data, mapguid) {
    // data returned from events are in JSON format, and should be processed with a JSON parser,
    // but eval will work for demonstration purposes
    var result = eval(‘(‘ + data + ‘)’);

    // we want to be sure that we are activating the correct one.
    var reg = new RegExp(sampleName + ".dll$");
    if (result.success && reg.test(result.plugInPath)) {
        objectPlugInGuid = result.guid;
        control3D.ActivatePlugIn(objectPlugInGuid, null);
        isLoaded = true;
    }
}

function FlyToAirport() {
    if (isLoaded) {
        var airport = document.getElementById(‘airports’).options[document.getElementById(‘airports’).selectedIndex].value;
        control3D.RaiseEvent(objectPlugInGuid, "FlyToAirport", airport);
    }
    else
        alert("Please wait for plugin to load");
}

function ChangeCockpit() {
    if (isLoaded) {
        var cockpit = document.getElementById(‘cockpits’).options[document.getElementById(‘cockpits’).selectedIndex].value;
        control3D.RaiseEvent(objectPlugInGuid, "ChangeCockpit", cockpit);
    }
    else
        alert("Please wait for plugin to load");
}       
</script>

The body of your page should look like this:

<body onload="OnPageLoad();" style="background-color:Black;">
<table>
    <tr><td colspan="2" align="center" style="color:White;"><h2>VE 3D Simple Flight Simulator</h2></td></tr>
    <tr>
        <td align="center">
            <select id="airports">
                <option value="LasVegas">Las Vegas International</option>
                <option value="newyorkNewark">Newark Liberty International (New York)</option>
                <option value="seattleTacoma">Seattle-Tacoma International</option>
                <option value="TorontoPearson">Pearson Internalional (Toronto)</option>
            </select>
            <input type="button" value="Go To Airport" onclick="FlyToAirport();" />
        </td>
        <td align="center">
            <select id="cockpits">
                <option value="cockpit1">Cockpit 1</option>
                <option value="cockpit2">Cockpit 2</option>
                <option value="cockpit3">Cockpit 3</option>
            </select>
            <input type="button" value="Change Cockpit" onclick="ChangeCockpit();" />
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <div id="myMap" style="position:relative;width:1024;height:768;"></div>
        </td>
    </tr>
</table>
</body>


Part 1 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!756.entry
Part 2 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!752.entry
Part 4 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!750.entry

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s