VB.Net Set Camera, Google Earth API

This article explains how you can use VB.Net to automate Google Earth and set the camera to the location you want. It will be assumed there is KML file located in the path “D:StuffBusinessTempLocation.KML”. The KML file contains the required data for setting the camera position such as the Longitude, Latitude, Altitude, …. parameters.

In order for the code in this article to work you will need to add reference to the following libraries:

  • Microsoft XML ( i.e Microsoft XML V6.0)
  • Google Earth 1.0 Type Library

The code has 3  parts:

  • Start google earth
  • Get KML file properties
  • Set the camera

I have explained the first 2 parts in the articles below:

The code below achieves this:

Sub main()
'google application
Dim objGoogle As EARTHLib.ApplicationGE
'XML object
Dim objXML As MSXML2.DOMDocument60
'KML File properties
Dim dblLong As Double
Dim dblLat As Double
Dim dblAlt As Double
Dim dblHeading As Double
Dim dbltitl As Double
Dim dblrange As Double
Dim straltiMode As String
Dim objAltMode As EARTHLib.AltitudeModeGE
Dim objNode As MSXML2.IXMLDOMNode

'automate XML object
objXML = New MSXML2.DOMDocument60
'automate google earth
objGoogle = New EARTHLib.ApplicationGE
'wait for google earth to load
Threading.Thread.Sleep (5000)
'load KML file
objXML.Load ("D:StuffBusinessTempLocation.KML")
'get KML file properties
objNode = objXML.ChildNodes.Item(1).ChildNodes.Item(0 _
).ChildNodes.Item(4).ChildNodes.Item(1)
dblLong = objNode.ChildNodes.Item(0).Text
dblLat = objNode.ChildNodes.Item(1).Text
dblAlt = objNode.ChildNodes.Item(2).Text
dblHeading = objNode.ChildNodes.Item(3).Text
dbltitl = objNode.ChildNodes.Item(4).Text
dblrange = objNode.ChildNodes.Item(5).Text
straltiMode = objNode.ChildNodes.Item(6).Text
If straltiMode = "relativeToSeaFloor" Then
objAltMode = EARTHLib.AltitudeModeGE.AbsoluteAltitudeGE
Else
objAltMode = EARTHLib.AltitudeModeGE.RelativeToGroundAltitudeGE
End If
'set the campera
Call objGoogle.SetCameraParams(dblLat, dblLong, dblAlt, _
objAltMode, dblrange, dbltitl, 0, 0.7)
End Sub

Result:
Set Camera


Code Explanation:

The code below automates google earth:

objGoogle = New EARTHLib.ApplicationGE
'wait for google earth to load  
Threading.Thread.Sleep (5000)

The code below gets the different properties from the KML file (Longitude, Latitude, Altitude, …):

objXML.Load ("D:StuffBusinessTempLocation.KML")
'get KML file properties
objNode = objXML.ChildNodes.Item(1).ChildNodes.Item(0 _
).ChildNodes.Item(4).ChildNodes.Item(1)
dblLong = objNode.ChildNodes.Item(0).Text
dblLat = objNode.ChildNodes.Item(1).Text
dblAlt = objNode.ChildNodes.Item(2).Text
dblHeading = objNode.ChildNodes.Item(3).Text
dbltitl = objNode.ChildNodes.Item(4).Text
dblrange = objNode.ChildNodes.Item(5).Text
straltiMode = objNode.ChildNodes.Item(6).Text

The line below moves the camera to the selected location. The last parameter is the camera speed:

'set the campera
Call objGoogle.SetCameraParams(dblLat, dblLong, dblAlt, _
objAltMode, dblrange, dbltitl, 0, 0.7)

You can download the file and code used in this article from the link below:

Leave a Reply

Your email address will not be published. Required fields are marked *