Tuesday, January 29, 2008

Create Your Own Google Earth Application

Building an application to work with Google Earth is pretty easy to do. This article will walk through the creation of a very simple application to capture different views in Google Earth.

To begin with, you will need to download Microsoft's Visual Studion Express Edition for C# (available for free at http://www.microsoft.com/express/vcsharp/).

Once Visual Studio is installed, select 'New Project' from the 'File' menu. In the New Project dialog box, select 'Windows Forms Application' and name your application 'FirstGeApp' (see below). Then select OK.




Once the new form is visible, right click on it and select 'Properties'. A properties window should appear in the lower right hand corner of Visual Studio. Change the following Form properties:

Name = frmViewCapture



Text = View Capture



TopMost = True



The TopMost property will keep your form on top of all other applications. This works well when interacting with Google Earth.




Once you have set the Form properties, we will add a MenuStrip control to the form, to do this, click on the Toolbox on the left side of the Visual Studio. When the tools appear, click on the pushpin icon to keep the tools open (see above).



After this, look for the 'MenuStrip' control and drag it onto your form.





Once the MenuStrip is on your Form, click on it to enter text. In the example below, I have added a 'File' menu and a "Capture View" menu. You can keep the default setting for both of these.





Next, we are going to add a TreeView control to store our captured views. Find TreeView from the Toobox pallete and drag it onto the form. Once you have done so, change the 'Dock' property of the TreeView to "Fill" (see bel0w). This will make the TreeView take up our whole Form.



Next, add an ImageList control. You can drag this control onto the Form, or drag it to the bottom part of Visual Studio (see below).



After you have added the ImageList, right click on and select 'Choose images' (see above). In the next dialog box, select "Add". Navigate to an icon of your choice (I used
C:\Program Files\Google\Google Earth\GoogleEarth.ico).

Once you have added the image, click on the TreeView in the form and look in the Properties on the right. Set the following properties:

ImageList = imageList1
ImageIndex = 0

Next, you will need to add a reference to Google Earth. To do so, look for 'References' in the Solution Explorer in the upper right hand panel of Visual Studio. Right click References and select "Add Reference". In the dialog box that comes up, select the 'Browse' tab. Browse to
C:\Program Files\Google\Google Earth\googleearth.dll (depending on your installation) and add it as a reference.

Next, click on Form1 in the Solution Explorer and select Code View (see below).


Now we are going to add some code. You will see code similar to below. Add the underlined code to project.


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


namespace FirstGeApp
{
public partial class frmViewCapture : Form
{
//Add a reference to the Google Earth Library
private EARTHLib.ApplicationGEClass _ge = new EARTHLib.ApplicationGEClass();
//Make an integer to keep track of our Google Earth Views
private int _viewNum = 0;


public frmViewCapture()
{
InitializeComponent();
}
}
}


Next, we will nee an event handler to capture the view. The simplest way to do this is to go back to your form and double-click "Capture View" on the menu that you added to your form. Visual Studio will automatically add the code below to handle the click event for this menu item.


private void captureViewToolStripMenuItem_Click(object sender, EventArgs e)
{

}


Next, we will add some code to actually handle this event. Write the following method underneath the captureViewToolStripMenuItem_Click method.


private void captureGeView()
{
//Get the current Google Earth Camera
EARTHLib.CameraInfoGE cam = _ge.GetCamera(1);
//Increment the number of views
_viewNum++;
//Create a Node for the tree view
TreeNode node = new TreeNode("View " + _viewNum.ToString());
//Add a reference the the GE Camera to the node
node.Tag = cam;
//Add the node to the tree view
this.treeView1.Nodes.Add(node);
}


Now, add "captureGeView();" to the captureViewToolStripMenuItem_Click method.


The next step is to add code to actually move Google Earth to each of the capture views. Go back to the Form and click on the TreeView control (treeView1). On top of the properties box, you will see an icon that looks like a bolt of lightening. Select this to see events for the TreeView. Double Click on MouseDown to automatically add a handler (see below).



This will add a "treeView1MouseDown" method to your code. Add the following code in this method.



private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
//Get the clicked node
TreeNode node = this.treeView1.GetNodeAt(new Point(e.X, e.Y));
if (node != null)
{
//Get the cam from the tree view tag reference
EARTHLib.CameraInfoGE camera = (EARTHLib.CameraInfoGE)node.Tag;
//Move the GE View
moveGE(camera);
}
}


And the following


private void moveGE(EARTHLib.CameraInfoGE cam)
{
//Set the Google Earth Camera View

_ge.SetCamera(cam, .5); //Change this number to alter your speed
}


Now Press F5 on your keyboard to run. Google Earth will open with your application. Clicking "Capture View" will add a view to your Tree View. Clicking on a node on your TreeView will move you to that node


Can you name this stadium?


You can take this much farther by adding user input to name each view and alter the speed. Chain them together to creat a tour or export to KML.

51 comments:

  1. Very helpful for me. Thanks

    ReplyDelete
  2. Hi, thank you for example, it helped me make my first windows application controling Google Earth.

    Nevertheless I have problem with occasional error report such as:

    ************** Exception Text **************
    System.ArgumentException: Value does not fall within the expected range.
    at EARTHLib.ApplicationGEClass.SetCamera(CameraInfoGE camera, Double speed)

    I am wondering how it is possible, since I get the camera object at certain time and afterwards put the same object by setCamera.

    Ok, maybe the parameter "speed" is wrong, but how can I know what the problem is?

    ReplyDelete
  3. Anonymous12:43 AM

    Great example, thank you!

    ReplyDelete
  4. Anonymous3:22 AM

    The sample is a great.

    The Argument exception mentioned by oggy seems to result from negative tilt values coming from Google Earth.
    I simply added

    if (cam.Tilt < 0)
    cam.Tilt = Math.Abs(cam.Tilt);

    to the moveGE function and it worked.

    ReplyDelete
  5. Hi, great stuff. I wanna know if I can change the language to VB instead of C#?

    ReplyDelete
  6. Using VB to do this should not be a problem.

    ReplyDelete
  7. This help me, added with this article : http://www.xtremevbtalk.com/showthread.php?t=287038

    I think we can create a nice "custom" GE application.

    Can we add placemark programmatically using VB.NET?

    ReplyDelete
  8. I know its basic I just want to know one thing; how do i open the google earth window in the VB application? Do I use IsInitialised from IApplicationGE?

    ReplyDelete
  9. It seems that once you create a new reference to the ApplicationGEClass, Google Earth will automatically launch if it is not already open.
    C#
    private EARTHLib.ApplicationGEClass _ge = new EARTHLib.ApplicationGEClass();
    VB
    Dim _ge as EARTHLib.ApplicationGEClass = New EARTHLib.ApplicationGEClass();

    You cannot have more than one instance of GE open at a time.

    If you want to open the Google Earth window to run inside of VB .. that's a different story, but entirely possible.

    ReplyDelete
  10. Hi,

    I installed vc# 2008 just to try this out but the googleearth.dll is missing in de googleearth map. Where is it. I also installed google earth 4.3 but it is still missing..

    Why ?
    Regards

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. It seems that at version 4.3, there is no longer a googlearth.dll. You should be able to use the googleearth.exe and get the same results.

    ReplyDelete
  13. Anonymous11:39 AM

    The example is very nice .Thanks
    I have a problem with
    using System.Linq;
    I can not add the System.Linq reference (greyed out). I am using VS 2008

    ReplyDelete
  14. Anonymous1:31 PM

    Hi
    Do you know if I can superimpose moving objects from my .NET application over the Google Earth as a background ?
    thanks

    ReplyDelete
  15. Do you want to just move points?
    If so,
    The best way to move objects is to:
    1. Create a kml file with points to represent your moving objects.
    2. Create a network link kml file that points to the location of your point kml file.
    3. Load the network link kml file using the GE api.
    4. Use a timer to rewrite the point kml with the update coordinates.
    5.Reload the network link file.

    The reason you use a network link is that you can tell it not to move when the new data is loaded.

    ReplyDelete
  16. So you say is possible open GEarth inside .NET app.
    I'm searching for a sample. Could you helpme with a sample code (VB ou c#) or tutorial?

    I need have a form with gearth funcionalities, like:
    - show gearth images abble to capture lat x lon click point;
    - show an icon in as especific point...

    please, help me if you can.

    tks

    Carlos Barini (Brasil)
    cabarini@gmail.com

    ReplyDelete
  17. Anonymous1:15 PM

    Does someone have details on how to overlay an image on top of Google Earth. There was some mention of KML files. Cheers

    ReplyDelete
  18. Overlaying an image is actually pretty easy. You can do it in the free client by adding an image overlay, and referencing the location of the image.

    ReplyDelete
  19. Anonymous8:41 AM

    How can I move an overlaid image from my C# application?
    Generally, how can I include a placemark that was created by GE client into my application.

    ReplyDelete
  20. Anonymous6:33 AM

    Hi, when i run the program, GE opens up perfectly, but clicking on "Capture View" on my form seems to only place the small image, "google_earth.ico", on the tree view of the form, and nothing else. Does anybody have any idea to solve this problem? Perhaps i missed something out?

    thanks

    ReplyDelete
  21. Anonymous6:48 AM

    is there anyway to view GE live in the form itself?

    ReplyDelete
  22. Anonymous8:21 AM

    Some knows how to convert this Java into C# ? I want to set the style of a line placemark (color & width).
    Following is in Java:
    var lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle(); lineStyle.setWidth(lineStyle.getWidth() + 2); lineStyle.getColor().set('66ff0000');
    thanks

    ReplyDelete
  23. Anonymous4:05 PM

    I am looking to create an application where I can put an icon on google earth that corresponds to an avalanche and then 'export'? it so others can see it (and all the other avalanches over the course of the winter). And each icon can be a hyperlink for a photo or something else. Am I in the right place for learning how to do this?

    ReplyDelete
  24. Hi, I need to get googleearth.dll, please help with the link. Infect i've tried to download google earth but there was no Googleearth.dll.

    Thankx
    "General"

    ReplyDelete
  25. Anonymous2:41 PM

    WOW! This was great.
    Thanks so much for going to all this trouble, I really appreciate it.
    It was an excelent start.
    I was able to reference the exectable and it worked fine.
    Two questions:
    1) Has anyone figured out a way to cloe Google Earth when the C# application ends?

    2) Is there a complete refernce to the Google Earth API/

    Thanks,
    John

    ReplyDelete
  26. Hey, great sample and great blog. I'm going to have a play with this later...BTW I went about it a different way - using the GEPlugin.

    http://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html

    and

    http://fraserchapman.blogspot.com/2008/12/google-earth-plugin-control-library.html

    Regards,

    F

    ReplyDelete
  27. Anonymous9:12 AM

    I have a problem. When I create a new ApplicationGE object, no problem. But when I go to Capture View and I call _ge.GetCamera(), a Windows Installer window pops up on my screen saying, "Please wait while Windows configures Google Earth."

    How do I make that go away?

    B

    ReplyDelete
  28. Anonymous2:49 PM

    I also can't find the googleearth.dll. I have added the reference Google Earth 1.0 Type Library. I have the Imports EARTHLib to my VB.NET application. I have searched for the googleearth.dll file in the web but not much useful info out there. Any ideas?

    ReplyDelete
  29. Referencing the googleearth.exe file should work the same as the googleearth.dll

    ReplyDelete
  30. Tom West4:27 AM

    Just adding my thanks for this demo. It was exactly what I needed to get started.

    ReplyDelete
  31. Anonymous8:02 AM

    I want to add the google earth on my website which I am making using asp.net with c# . Can anyone...give me some way...to do the above

    thanks...

    ReplyDelete
  32. Anonymous9:03 AM

    My version of Google Earth don't have this DLL. But I've found another very useful link :

    http://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html

    Hope this helps too.

    ReplyDelete
  33. Anonymous4:52 AM

    very useful articel, thanks for that.
    any idea how to delete all placemarks from temp places using c#?

    ReplyDelete
  34. Anonymous4:55 AM

    hey e verybody,
    I have a problem: I want to clean the GE from placemarks in temp places I put there previously. Any ideas?

    ReplyDelete
  35. Anonymous9:44 PM

    i imported this file:

    C:\Program Files\Google\Google Earth\googleearth.exe

    ReplyDelete
  36. Anonymous4:51 PM

    Hi, I run the prg and everything runs smoothly:
    Google Earth opens in new window.
    I press 'capture' and prg creates new views.
    But when I click on any view on the tree I get an exception on _ge.SetCamera(cam, .1)

    Please tell me what is wrong?

    ReplyDelete
  37. Anonymous11:29 AM

    regarding this exception
    ************** Exception Text **************
    System.ArgumentException: Value does not fall within the expected range.
    at EARTHLib.ApplicationGEClass.SetCamera(CameraInfoGE camera, Double speed)


    There seems to be a bug in GE 5.1 beta
    The following code throws the above exception

    CameraInfoGE cam = _ge.GetCamera(1);
    _ge.SetCamera(cam);


    To work-around this problem, set an accepted value
    for the FocusPointAltitudeMode before calling SetCamera:

    cam.FocusPointAltitudeMode = AltitudeModeGE.RelativeToGroundAltitudeGE;

    For some reason, GE 5.1 beta is sending an altitudeMode value of 5 when calling GetCamera()

    ReplyDelete
  38. It Is Not Working In Asp.net 2.0

    private EARTHLib.ApplicationGEClass _ge = new EARTHLib.ApplicationGEClass();
    This Gives Error Please Help'
    Retrieving the COM class factory for component with CLSID {8097D7E9-DB9E-4AEF-9B28-61D82A1DF784} failed due to the following error: 80040154.'

    ReplyDelete
    Replies
    1. Just open the Properties tab for the EARTHLib reference and set "Embed Interop Types" to "False"

      Delete
  39. I'm not sure if this is related to your problem, but there has been a problem with a recent version of Google Earth not registering correctly. See
    http://tinyurl.com/yg2ktt9

    If you are using ASP.NET, have you looked at the plugin API?
    http://code.google.com/apis/earth/

    ReplyDelete
  40. Antonis3:27 AM

    Thanks for this great atricle

    If anybody can not find the googleearth.dll or if it does not work you can use the googleearth.exe in the plugins folder

    ReplyDelete
  41. So how do you add things like points and such to the google earth map.

    ReplyDelete
  42. Anonymous10:43 AM

    How to start google earth programatically in C sharp?

    ReplyDelete
  43. hey
    when i add googleearth.exe as reference, it doesn't load any code in form code view.
    should it be located in the windows form application bin folder? what might be the problem?
    *note: I'm working with VB

    ReplyDelete
  44. Anonymous12:43 PM

    For those of you who cannot find googleearth.dll, that is because you installed Google Earth Plugin only.

    There is no googleearth.dll file from plugin.
    You will see only googleearth_free.dll at C:\Program Files\Google\Google Earth\plugin, which is not the right one.

    You have to install Google Earth itself, which will add 'client' folder under C:\Program Files\Google\Google Earth\
    You will see googleearth.exe in there, and you need to use that file as a reference.

    ReplyDelete
  45. Anonymous2:09 AM

    cannot convert from 'int' to 'EARTHLib.AltitudeModE'

    i am setting values as
    ge.SetCameraParams( 40.777, -111.8882, 0.0, 1, 500.00, 40.00, 0.0, 0.1);
    but error came at 4th place..pls help

    ReplyDelete
    Replies
    1. Anonymous12:21 PM

      Try "EARTHLib.AltitudeModeGE.RelativeToGroundAltitude"

      Delete
  46. interop type EARTHLib.ApplicationGEClass cannot be embedded Error with C:\Program Files\Google\Google Earth\Client\GoogleEarth.exe

    ReplyDelete
  47. Anonymous1:05 AM

    same here Vassili, have you found maybe the answer? i will be very grateful if you would share the problem.

    ReplyDelete
  48. Hi: I would like to contact with an advanced freelance developer over Google Earth. We want to develop a mockup environment with GE, to view different scenarios based on a "Drag and drop" library of objects (buildings, cars, brigges, trees, people)
    Example: Age of Empires Scenario editor, but based on Google Earth.
    miguel.alcalde@bull.es

    Regards.

    ReplyDelete
  49. this is my code which worked

    1) i included in references C:\Users\**your name NAME**\documents\visual studio 2010\Projects\FirstGeApp\FirstGeApp\obj\x86\Debug\Interop.EARTHLib.dll

    2) private EARTHLib.ApplicationGE _ge = new EARTHLib.ApplicationGE();

    instead of
    private EARTHLib.ApplicationGEClass _ge = new EARTHLib.ApplicationGEClass();

    3) also included cam.FocusPointAltitudeMode = AltitudeModeGE.RelativeToGroundAltitudeGE;

    it worked thanks a lot********


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using EARTHLib;

    namespace FirstGeApp
    {
    public partial class frmViewCapture : Form
    {

    //Add a reference to the Google Earth Library
    private EARTHLib.ApplicationGE _ge = new EARTHLib.ApplicationGE();
    //Make an integer to keep track of our Google Earth Views
    private int _viewNum = 0;



    public frmViewCapture()
    {
    InitializeComponent();
    }

    private void captureVIewToolStripMenuItem_Click(object sender, EventArgs e)
    {
    captureGeView();
    }


    private void captureGeView()
    {
    //Get the current Google Earth Camera
    EARTHLib.CameraInfoGE cam = _ge.GetCamera(1);
    //Increment the number of views
    _viewNum++;
    //Create a Node for the tree view
    TreeNode node = new TreeNode("View " + _viewNum.ToString());
    //Add a reference the the GE Camera to the node
    node.Tag = cam;
    //Add the node to the tree view
    this.treeView1.Nodes.Add(node);
    }

    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
    //Get the clicked node
    TreeNode node = this.treeView1.GetNodeAt(new Point(e.X, e.Y));
    if (node != null)
    {
    //Get the cam from the tree view tag reference
    EARTHLib.CameraInfoGE camera = (EARTHLib.CameraInfoGE)node.Tag;
    //Move the GE View
    moveGE(camera);
    }
    }

    private void moveGE(EARTHLib.CameraInfoGE cam)
    {
    //Set the Google Earth Camera View
    cam.FocusPointAltitudeMode = AltitudeModeGE.RelativeToGroundAltitudeGE;
    _ge.SetCamera(cam, 2); //Change this number to alter your speed
    }
    }
    }

    ReplyDelete