Click here to Skip to main content
13,202,262 members (66,904 online)
Click here to Skip to main content
Add your own
alternative version


136 bookmarked
Posted 8 Dec 2008

Embedding Google Earth in a C# Application

, 8 Dec 2008
Rate this:
Please Sign up or sign in to vote.
How to embed the Google Earth scene in a C# application.


If you're interested in utilizing the Google Earth viewer as a control in a custom application, then continue reading. There are quite a few links around the Internet that provide information as to how to add references to your project, create a ApplicationGE, and control the Google Earth application. However, most of these tutorials do not focus on the details of embedding Google Earth in an application. That's what I will focus on here. From now on, GE means Google Earth.

The Code

You'll probably start off on your embedding adventure much like I did, with:

EARTHLib.ApplicationGE ge = new ApplicationGEClass();

If the GE process already exists, we will obtain a reference to it; if not, a new process will be created, and you would see the GE logo flash on the screen while loading. You will then see the main Google Earth screen with the embedded viewer. Since we are interested in embedding GE, we are not interested in having the main screen around, so we hide it!

ShowWindowAsync(ge.GetMainHwnd(), 0);

For all intensive purposes, we now have an empty screen. What we'd like to do next is embed the scene that GE renders into our application. We accomplish this by setting the render Hwnds parent to that of the window we would like to render to:

SetParent(ge.GetRenderHwnd(), this.Handle.ToInt32());

In the example above, 'this' is a Windows Form. The end result, if you perform these same steps, should look similar to the image below, although I do warn that results may vary :-)


You will notice that resizing the window has no effect on the scene. If you plan on embedding GE in an application for any useful purposes, you'll most likely need it to respond to resizing. I spent a bit of time sniffing into the events that were passed to the scene when I performed re-size of the main GE application window. This led me to a special event, WM_QT_PAINT, in addition to a sequence of others. It took a bit of tinkering to get it all right, but this appeared to work for me:

SendMessage(ge.GetMainHwnd(), WM_COMMAND, WM_PAINT, 0);
PostMessage(ge.GetMainHwnd(), WM_QT_PAINT, 0, 0);
SetWindowPos( ge.GetMainHwnd(), HWND_TOP, 0, 0, (int)this.Width, 
             (int)this.Height, SWP_FRAMECHANGED);
SendMessage(ge.GetRenderHwnd(), WM_COMMAND, WM_SIZE, 0);

This should allow the scene to adjust according to the parent form's size. I bundled this up into a method named "ResizeGoogleControl", and called it after SetParent and within my Form_Resize event. The results are illustrated in this image:


The Example

I slapped together a test app that you are welcome to use for reference. If you have any comments or suggestions, be sure to send me an email or add a comment. Create a new C# project in Visual Studio and replace the Form1.cs code with this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

using EARTHLib;

namespace resize_google_earth

public partial class Form1 : Form
    private static extern int SetParent(
    int hWndChild,
    int hWndParent);

    private static extern bool ShowWindowAsync(
    int hWnd,
    int nCmdShow);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool PostMessage(
    int hWnd,
    uint Msg,
    int wParam,
    int lParam);

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    private static extern bool SetWindowPos(
    int hWnd,
    int hWndInsertAfter,
    int X,
    int Y,
    int cx,
    int cy,
    uint uFlags);

    private static extern int SendMessage(
    int hWnd,
    uint Msg,
    int wParam,
    int lParam);

    private const int HWND_TOP = 0x0;
    private const int WM_COMMAND = 0x0112;
    private const int WM_QT_PAINT = 0xC2DC;
    private const int WM_PAINT = 0x000F;
    private const int WM_SIZE = 0x0005;
    private const int SWP_FRAMECHANGED = 0x0020;

    public Form1()

        ge = new ApplicationGEClass();

        ShowWindowAsync(ge.GetMainHwnd(), 0);
        SetParent(ge.GetRenderHwnd(), this.Handle.ToInt32());

    private void Form1_Resize(object sender, EventArgs e)

    private void ResizeGoogleControl()
        SendMessage(ge.GetMainHwnd(), WM_COMMAND, WM_PAINT, 0);
        PostMessage(ge.GetMainHwnd(), WM_QT_PAINT, 0, 0);


        SendMessage(ge.GetRenderHwnd(), WM_COMMAND, WM_SIZE, 0);

    private EARTHLib.ApplicationGE ge = null;


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Joseph Armbruster
Software Developer
United States United States
I love to write programs and vim is my editor of choice. Programming languages fascinate me and I feel at least reasonably familiar with C, python, C++, JAVA, C#, ECMAScript, Lua and Ruby. I also use a few open source relational database systems; postgresql, mysql, sqlite. Some of my interests include: programming languages, geoprocessing, number theory, network protocols, web services, cryptography, linux, windows, open source, GIS.


You may also be interested in...


Comments and Discussions

QuestionWhere to find more? Pin
meridbt24-Mar-15 23:48
membermeridbt24-Mar-15 23:48 
QuestionThank you!!! Pin
eugenia monfort16-Dec-14 2:02
membereugenia monfort16-Dec-14 2:02 
QuestionI have two errors while building your code. Both in string "ge = new ApplicationGEClass();": 1. Error 1 The type 'EARTHLib.ApplicationGEClass' has no constructors defined 2. Error 2 Interop type 'EARTHLib.ApplicationGEClass' cannot be embedded. Us Pin
hknern14-Dec-14 23:17
memberhknern14-Dec-14 23:17 
AnswerRe: I have two errors while building your code. Both in string "ge = new ApplicationGEClass();": 1. Error 1 The type 'EARTHLib.ApplicationGEClass' has no constructors defined 2. Error 2 Interop type 'EARTHLib.ApplicationGEClass' cannot be embedded Pin
meridbt23-Mar-15 23:57
membermeridbt23-Mar-15 23:57 
QuestionSome Questions Pin
Mollaahmadi8-Sep-14 19:49
memberMollaahmadi8-Sep-14 19:49 
BugGoogleEarth process isn't closed after form closing! Pin
Member 1088091513-Jun-14 2:11
memberMember 1088091513-Jun-14 2:11 
GeneralRe: GoogleEarth process isn't closed after form closing! Pin
Zibusiso22-Aug-14 2:31
memberZibusiso22-Aug-14 2:31 
QuestionAn errors occured while building Pin
Member 1088091512-Jun-14 23:39
memberMember 1088091512-Jun-14 23:39 
AnswerRe: An errors occured while building Pin
Member 1088091513-Jun-14 1:27
memberMember 1088091513-Jun-14 1:27 
QuestionPlease post LAT and LONG example for specific location.... Pin
zeeshan00917-Apr-14 9:38
memberzeeshan00917-Apr-14 9:38 
AnswerRe: Please post LAT and LONG example for specific location.... Pin
Member 1088091513-Jun-14 2:41
memberMember 1088091513-Jun-14 2:41 
QuestionEARTHLib Pin
Member 105766996-Feb-14 4:30
memberMember 105766996-Feb-14 4:30 
AnswerRe: EARTHLib Pin
namth2994-Apr-14 22:34
membernamth2994-Apr-14 22:34 
QuestionForm not loading Google Earth Pin
Member 842394629-Apr-13 10:09
memberMember 842394629-Apr-13 10:09 
QuestionAdd GPS Pin
3nZo23-Apr-13 1:37
member3nZo23-Apr-13 1:37 
QuestionDear Sir, Pin
Ethiopaninomaltt23-Feb-13 2:51
memberEthiopaninomaltt23-Feb-13 2:51 
QuestionHow to get Coordiantes from cursor Pin
M.S.S.E21-Jan-13 23:57
memberM.S.S.E21-Jan-13 23:57 
QuestionWPF Compatibilty Pin
Member 960766617-Nov-12 8:23
memberMember 960766617-Nov-12 8:23 
QuestionThis demo just open once , GE dont run in second Pin
shindo2165-Sep-12 17:48
membershindo2165-Sep-12 17:48 
AnswerRe: This demo just open once , GE dont run in second Pin
shindo2165-Sep-12 18:21
membershindo2165-Sep-12 18:21 
GeneralRe: This demo just open once , GE dont run in second Pin
raddog14-Jan-13 12:54
memberraddog14-Jan-13 12:54 
QuestionMouse wheel zoom in/out doesn't work? Pin
sherifffruitfly23-Aug-12 18:40
membersherifffruitfly23-Aug-12 18:40 
Questionplace mark icon Pin
mojimoj18-Mar-12 8:52
membermojimoj18-Mar-12 8:52 
QuestionHi Pin
myazar16-Nov-11 9:31
membermyazar16-Nov-11 9:31 
Hi Joseph,

Thanks for your sharing.

I downloaded your code and i ran in Microsoft Visual Studio 2010 but i am getting the following error.


Retrieving the COM class factory for component with CLSID {8097D7E9-DB9E-4AEF-9B28-61D82A1DF784} failed due to the following error: 80040154.

Could you please help me about this problem.

GeneralMy vote of 4 Pin
spicolihill@hotmail.com27-May-11 11:44
memberspicolihill@hotmail.com27-May-11 11:44 
Generalx64 (64bit) machines Pin
th3_v0ice1-May-11 6:10
memberth3_v0ice1-May-11 6:10 
GeneralInitializeComponent() Pin
nat0626-Jan-11 21:00
membernat0626-Jan-11 21:00 
GeneralRe: InitializeComponent() Pin
RayChenMedical21-Apr-11 6:25
memberRayChenMedical21-Apr-11 6:25 
GeneralEARTHLib; Pin
mostafa.elsadany7-Jan-11 4:28
membermostafa.elsadany7-Jan-11 4:28 
GeneralRe: EARTHLib; Pin
fosterz3-Feb-11 19:34
memberfosterz3-Feb-11 19:34 
GeneralRe: EARTHLib; Pin
MarkyMark7831-Dec-11 0:32
memberMarkyMark7831-Dec-11 0:32 
GeneralMy vote of 5 Pin
crazycoding20-Dec-10 1:05
membercrazycoding20-Dec-10 1:05 
QuestionChange the rendering size Pin
sdecorme6-Dec-10 23:17
membersdecorme6-Dec-10 23:17 
General[Help]how can close KML files. Pin
evhunter25-Nov-10 4:19
memberevhunter25-Nov-10 4:19 
GeneralMy vote of 5 Pin
Abhinav S22-Oct-10 6:40
memberAbhinav S22-Oct-10 6:40 
QuestionHow to get to a particular latitude and longitude Pin
hemshankarsahu20-Oct-10 20:37
memberhemshankarsahu20-Oct-10 20:37 
AnswerRe: How to get to a particular latitude and longitude Pin
sdecorme6-Dec-10 23:20
membersdecorme6-Dec-10 23:20 
GeneralMy vote of 5 Pin
gvmohanraj13-Oct-10 22:59
membergvmohanraj13-Oct-10 22:59 
QuestionDo you have sample code with setcamera and how to resize? Pin
5mayfive3-Oct-10 3:11
member5mayfive3-Oct-10 3:11 
GeneralGeez thnx dude this really works! Pin
Jan Kurbanaliev31-Aug-10 23:59
memberJan Kurbanaliev31-Aug-10 23:59 
GeneralEmpty Window Pin
Zimmermann Stefan13-Aug-10 4:42
memberZimmermann Stefan13-Aug-10 4:42 
GeneralRe: Empty Window Pin
kanutiger19-Aug-10 5:45
memberkanutiger19-Aug-10 5:45 
Questionwhere is the demo? Pin
wp873368413-Jul-10 6:31
memberwp873368413-Jul-10 6:31 
GeneralDownload of earthlib Pin
jonnesvik17-Jun-10 8:32
memberjonnesvik17-Jun-10 8:32 
GeneralRe: Download of earthlib Pin
kanutiger13-Aug-10 4:44
memberkanutiger13-Aug-10 4:44 
GeneralRe: Download of earthlib Pin
Member 129873518-Feb-17 3:41
memberMember 129873518-Feb-17 3:41 
General"Sniffing" Pin
Nate Trimble26-May-10 14:02
memberNate Trimble26-May-10 14:02 
QuestionTop Right nevigation buttion is not displaying Pin
chandrapal23-Mar-10 4:18
memberchandrapal23-Mar-10 4:18 
QuestionHow to draw lines? Pin
wangchi16-Nov-09 22:40
memberwangchi16-Nov-09 22:40 
Questionhow about mousewheel? Pin
afoolofhn1-Nov-09 0:56
memberafoolofhn1-Nov-09 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 8 Dec 2008
Article Copyright 2008 by Joseph Armbruster
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid