VBA Set Camera, Google Earth API

This article explains how you can use VBA for Excel 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:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub main()
'Google Earth object
Dim objgoogle As EARTHLib.ApplicationGE
'Microsoft XML Object
Dim objXML As MSXML2.DOMDocument60
'KML Data
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 objNode As MSXML2.IXMLDOMNode
Dim objAltMode As EARTHLib.AltitudeModeGE

'initialize objects
Set objXML = New MSXML2.DOMDocument60
Set objgoogle = New EARTHLib.ApplicationGE
Sleep 5000
'get data from the KML file
objXML.Load ("D:StuffBusinessTempLocation.kml")
Set 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 = AbsoluteAltitudeGE
Else
    objAltMode = RelativeToGroundAltitudeGE
End If
'set camera
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. The Sleep function allows google earth to launch before executing the rest of the code:

Set objgoogle = New EARTHLib.ApplicationGE
Sleep 5000

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

'get data from the KML file
objXML.Load ("D:StuffBusinessTempLocation.kml")
Set 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 = AbsoluteAltitudeGE
Else
    objAltMode = RelativeToGroundAltitudeGE
End If

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 *