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"
        <name>Sample Path</name>
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


Remember you'll need a CoInitialize before this.

Update: Steve is controlling Google Earth using JScript.


ip said...

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

cc Infopage said...


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,

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