Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Article

Embedding Google Earth in a C# Application

Rate me:
Please Sign up or sign in to vote.
4.78/5 (44 votes)
8 Dec 2008LGPL32 min read 350.7K   137   97
How to embed the Google Earth scene in a C# application.

Introduction

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:

C#
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!

C#
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:

C#
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 :-)

google_earth_embedded.jpg

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:

C#
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:

google_earth_embedded_good.jpg

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:

C#
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
{
    [DllImport("user32.dll")]
    private static extern int SetParent(
    int hWndChild,
    int hWndParent);

    [DllImport("user32.dll")]
    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);

    [DllImport("user32.dll")]
    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()
    {
        InitializeComponent();

        ge = new ApplicationGEClass();

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

    private void Form1_Resize(object sender, EventArgs e)
    {
        ResizeGoogleControl();
    }

    private void ResizeGoogleControl()
    {
        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);
    }

    private EARTHLib.ApplicationGE ge = null;
}
}

License

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


Written By
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.

See: http://www.joevial.com

Comments and Discussions

 
GeneralRe: Problem with the size Pin
xam_163@163.com17-Mar-09 22:33
xam_163@163.com17-Mar-09 22:33 
QuestionRe: Problem with the size Pin
MathieuGardere19-Apr-09 17:11
MathieuGardere19-Apr-09 17:11 
QuestionRe: Problem with the size Pin
Cedric166933-Mar-11 23:19
Cedric166933-Mar-11 23:19 
Questionexcellent - what about placemarks? Pin
Josh_D17-Dec-08 7:09
Josh_D17-Dec-08 7:09 
AnswerRe: excellent - what about placemarks? [modified] Pin
xam_163@163.com17-Mar-09 17:02
xam_163@163.com17-Mar-09 17:02 
GeneralDon't punch me but... Pin
ronzulu16-Dec-08 1:23
ronzulu16-Dec-08 1:23 
GeneralRe: Don't punch me but... Pin
Joseph Armbruster22-Dec-08 0:28
Joseph Armbruster22-Dec-08 0:28 
GeneralVery nice Pin
Guido_d9-Dec-08 4:56
Guido_d9-Dec-08 4:56 
Very nice article, got my 5 stars!
GeneralRe: Very nice Pin
Joseph Armbruster9-Dec-08 8:12
Joseph Armbruster9-Dec-08 8:12 
GeneralProblem with Windows XP Pin
nixite8-Dec-08 23:05
nixite8-Dec-08 23:05 
GeneralRe: Problem with Windows XP Pin
Joseph Armbruster9-Dec-08 2:21
Joseph Armbruster9-Dec-08 2:21 
GeneralRe: Problem with Windows XP Pin
Joseph Armbruster9-Dec-08 4:13
Joseph Armbruster9-Dec-08 4:13 
GeneralRe: Problem with Windows XP Pin
nixite9-Dec-08 4:32
nixite9-Dec-08 4:32 
GeneralWhere is EARTHLib; Pin
Xerox48-Dec-08 22:57
professionalXerox48-Dec-08 22:57 
GeneralRe: Where is EARTHLib; Pin
Joseph Armbruster9-Dec-08 2:18
Joseph Armbruster9-Dec-08 2:18 
GeneralRe: Where is EARTHLib; Pin
Xerox49-Dec-08 21:00
professionalXerox49-Dec-08 21:00 
GeneralRe: Where is EARTHLib; Pin
code_discuss15-Dec-08 20:38
code_discuss15-Dec-08 20:38 

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.