Monday, July 18, 2005

Controlling Google Earth from C++

Here's a quick trick to peak inside an application: Do a #import somewhere in Visual Studio and then examine the resulting IDL in the .tlh file after the compile.

I did this for the free version of Google Earth and got a googleearth.tlh. This exposed a basic COM interface. There is not much to control, but the following will give you some idea. (Sorry about the formatting of the long string. It looks ugly, but I'm sure you get the idea. Any KML is valid in this string, by the way.)

IKHInterfacePtr googleEarth(__uuidof(KHInterface));

// testing LoadKml()
BSTR kmlData = ::SysAllocString(L"
    <Placemark>
        <name>Sample Path</name>
        <LookAt>
            <longitude>-112.0822680013139</longitude>
            <latitude>36.09825589333556</latitude>
            <range>2889.145007690472</range>
            <tilt>62.04855796276328</tilt>
            <heading>103.8120432044965</heading>
        </LookAt>
        <LineString>
            <tessellate>1</tessellate>
            <coordinates>
                -112.0814237830345,36.10677870477137,0,
                -112.0870267752693,36.0905099328766,0,
                -112.082378303451,36.06778704771370,0
            </coordinates>
        </LineString>
    </Placemark>");
googleEarth->LoadKml(&kmlData); // loads the string

// change the view to the above area
IKHViewInfoPtr viewInfo = googleEarth->currentView;
viewInfo->latitude = 36.06778704771370;
viewInfo->longitude = -112.0870267752693;
viewInfo->range = 5000;
viewInfo->tilt = 45;
viewInfo->azimuth = 0;
googleEarth->setView(viewInfo, 0.1); // 0.1 is the speed

// open a file
googleEarth->OpenFile("Test.kml");

googleEarth->Release();


Remember you'll need a CoInitialize before this.

Update: Steve is controlling Google Earth using JScript.

3 comments:

ip said...

Hello, greate example, but when i tried to call "SaveScreenShot" method it crashed my app :(

cc Infopage said...

Hello,

I am searching around for fresh information
for my cc Infopage.
This website contains 30,000 Information Pages about all kind of subjects.

I just wanted to let you know that I read your blog. I hope you enjoy "Blogging" as much as I do.

I wish you all the luck I can, keep the good work going!

Kind regards,
Jos
http://www.infopage.cc

Today's Tip: Visit cc Infopage about google

Anonymous said...

If you would just take your time to know your way around, you could discover