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.
Very helpful for me. Thanks
ReplyDeleteHi, thank you for example, it helped me make my first windows application controling Google Earth.
ReplyDeleteNevertheless 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?
Great example, thank you!
ReplyDeleteThe sample is a great.
ReplyDeleteThe 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.
Hi, great stuff. I wanna know if I can change the language to VB instead of C#?
ReplyDeleteUsing VB to do this should not be a problem.
ReplyDeleteThis help me, added with this article : http://www.xtremevbtalk.com/showthread.php?t=287038
ReplyDeleteI think we can create a nice "custom" GE application.
Can we add placemark programmatically using VB.NET?
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?
ReplyDeleteIt seems that once you create a new reference to the ApplicationGEClass, Google Earth will automatically launch if it is not already open.
ReplyDeleteC#
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.
Hi. I need open more than once instance of GE in C#, ¿any idea? Thanks!
DeleteHi,
ReplyDeleteI 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
This comment has been removed by the author.
ReplyDeleteIt 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.
ReplyDeleteThe example is very nice .Thanks
ReplyDeleteI have a problem with
using System.Linq;
I can not add the System.Linq reference (greyed out). I am using VS 2008
Hi
ReplyDeleteDo you know if I can superimpose moving objects from my .NET application over the Google Earth as a background ?
thanks
Do you want to just move points?
ReplyDeleteIf 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.
So you say is possible open GEarth inside .NET app.
ReplyDeleteI'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
Does someone have details on how to overlay an image on top of Google Earth. There was some mention of KML files. Cheers
ReplyDeleteOverlaying 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.
ReplyDeleteHow can I move an overlaid image from my C# application?
ReplyDeleteGenerally, how can I include a placemark that was created by GE client into my application.
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?
ReplyDeletethanks
is there anyway to view GE live in the form itself?
ReplyDeleteSome knows how to convert this Java into C# ? I want to set the style of a line placemark (color & width).
ReplyDeleteFollowing is in Java:
var lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle(); lineStyle.setWidth(lineStyle.getWidth() + 2); lineStyle.getColor().set('66ff0000');
thanks
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?
ReplyDeleteHi, 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.
ReplyDeleteThankx
"General"
WOW! This was great.
ReplyDeleteThanks 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
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.
ReplyDeletehttp://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
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."
ReplyDeleteHow do I make that go away?
B
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?
ReplyDeleteReferencing the googleearth.exe file should work the same as the googleearth.dll
ReplyDeleteJust adding my thanks for this demo. It was exactly what I needed to get started.
ReplyDeleteI 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
ReplyDeletethanks...
My version of Google Earth don't have this DLL. But I've found another very useful link :
ReplyDeletehttp://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html
Hope this helps too.
very useful articel, thanks for that.
ReplyDeleteany idea how to delete all placemarks from temp places using c#?
hey e verybody,
ReplyDeleteI have a problem: I want to clean the GE from placemarks in temp places I put there previously. Any ideas?
i imported this file:
ReplyDeleteC:\Program Files\Google\Google Earth\googleearth.exe
Hi, I run the prg and everything runs smoothly:
ReplyDeleteGoogle 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?
regarding this exception
ReplyDelete************** 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()
It Is Not Working In Asp.net 2.0
ReplyDeleteprivate 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.'
Just open the Properties tab for the EARTHLib reference and set "Embed Interop Types" to "False"
DeleteI'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
ReplyDeletehttp://tinyurl.com/yg2ktt9
If you are using ASP.NET, have you looked at the plugin API?
http://code.google.com/apis/earth/
Thanks for this great atricle
ReplyDeleteIf anybody can not find the googleearth.dll or if it does not work you can use the googleearth.exe in the plugins folder
So how do you add things like points and such to the google earth map.
ReplyDeleteHow to start google earth programatically in C sharp?
ReplyDeletehey
ReplyDeletewhen 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
For those of you who cannot find googleearth.dll, that is because you installed Google Earth Plugin only.
ReplyDeleteThere 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.
cannot convert from 'int' to 'EARTHLib.AltitudeModE'
ReplyDeletei 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
Try "EARTHLib.AltitudeModeGE.RelativeToGroundAltitude"
Deleteinterop type EARTHLib.ApplicationGEClass cannot be embedded Error with C:\Program Files\Google\Google Earth\Client\GoogleEarth.exe
ReplyDeletesame here Vassili, have you found maybe the answer? i will be very grateful if you would share the problem.
ReplyDeleteHi: 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)
ReplyDeleteExample: Age of Empires Scenario editor, but based on Google Earth.
miguel.alcalde@bull.es
Regards.
this is my code which worked
ReplyDelete1) 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
}
}
}
Hallo Guy
ReplyDeleteCongratulations again on this great tutorial
but i was wondering if anyone had faced difficulties adding the Google earth .dll file as reference . i am using VS2010 and the reference Addition does not go through --> ouput message : " myreference.dll could not be added , PLease make sure the file is accessible and that it is a valid assembly or COM components.
Really need help with this
Thanks again
hi to all
ReplyDeleteI Created a software in VB 2008 that in my "form1" when i click on "open image button" it display image on "picturebox1" with this
format: jpg,.gif|.png,bmp,tiff ,tag.,ppm.,pgm.,kml.
now i want in the same time "google earth" run, and this "picturebox1_image" overlay display in "google earth".
i can ran "google earth" but i don't know how to add, send or open image in "google earth".
i run google eath by 2 below code
Dim appGoogle As EARTHLib.ApplicationGE
appGoogle = New EARTHLib.ApplicationGE
now what do i do to open and display "picturbox1" in google earth?
Then once I have selected "picturbox1" image; what would be the appropriate code to put that image into google earth?
i wait to your help!
please tutorial me.
Once again; thanks for the help and any help is very much appreciated.
jatay1348@gmail.com