教你利用Google Earth API自动化谷歌地球代码

首先我们来了解一下Google Earth API是什么。Google 地球插件及其 JavaScript API 使您可以将 Google地图(一种真 3D 数字地球仪)嵌入到您的网页中。使用该 API 您可以绘制标记和折线、使图片覆盖区域、添加 3D 模型或加载 KML 文件,从而允许您构建复杂的 3D 地图应用程序。如果您有现有地图 API 站点,那么只需一行代码即可在您的网页上启用 3D 效果。

Google Earth相信大家都非常熟悉了,也经常会使用它,但是大家有没有想过来自动化Google Earth呢,今天我们就来使用QTP通过调用Google Earth的API来自动化Google Earth。在自动化Google Earth之前有几个必要条件需要达成,以下是ADVANCEDQTP上提供的脚本。大家可以试着跑一下,挺有意思的。

 

此Demo只适合浏览器中嵌入式的Google Earth
此Demo将改变视图类型并定位在纽约的时代广场

安装QTP
Web + ActiveX 插件加载
必须安装Google核心插件:http://code.google.com/intl/zh-CN/apis/earth/

Const URL = "http://earth-api-samples.googlecode.com/svn/trunk/demos/kmlplayground/index.html"

Const SW_MAXIMIZED = 3

' *****************关闭所有窗口和打开并跳转URL ***********************

SystemUtil.CloseProcessByName "iexplore.exe"

SystemUtil.Run "iexplore.exe", URL, "", "open", SW_MAXIMIZED

Browser("CreationTime:=0").Page("index:=0").Sync

Set objActiveX = Browser("CreationTime:=0").Page("index:=0")._

ActiveX("progid:=GEPluginCoClass.GEPluginCoClass.1.0", "location:=0").Object

' ****************设置变量的值***********************

ALTITUDE_ABSOLUTE = objActiveX.ALTITUDE_ABSOLUTE

VISIBILITY_SHOW = objActiveX.VISIBILITY_SHOW

ALTITUDE_RELATIVE_TO_GROUND = objActiveX.ALTITUDE_RELATIVE_TO_GROUND

MAP_TYPE_EARTH = objActiveX.MAP_TYPE_EARTH

MAP_TYPE_SKY = objActiveX.MAP_TYPE_SKY

SPEED_TELEPORT = objActiveX.SPEED_TELEPORT

wait 2

' ***************切换到天空视图 *****************

Set geOptions = objActiveX.getOptions()

Call geOptions.setMapType( MAP_TYPE_SKY )

wait 5

' ******************切换地球视图 *******************

Call geOptions.setMapType( MAP_TYPE_EARTH )

' Setting to Minimum zoom at max speed

geOptions.setFlyToSpeed( SPEED_TELEPORT )

Set kmlLookAt = objActiveX.getView().copyAsLookAt( ALTITUDE_ABSOLUTE )

kmlLookAt.setRange(50000000)

objActiveX.getView().setAbstractView(kmlLookAt)

wait 2

' ******************增加虚拟控件 *********************

geOptions.setGridVisibility( true )

geOptions.setStatusBarVisibility(true)

geOptions.setScaleLegendVisibility( true ) 

geOptions.setMouseNavigationEnabled( true )

geOptions.setOverviewMapVisibility( true )

' *******************检索导航控制 *******************

Set geNavigationControl = objActiveX.getNavigationControl()

geNavigationControl.setVisibility( VISIBILITY_SHOW )

Set geView = objActiveX.getView()

Set kmlLookAt = geView.copyAsLookAt( ALTITUDE_RELATIVE_TO_GROUND )

' ******************** 设置一些参数 *******************

geOptions.setFlyToSpeed(0.5)

kmlLookAt.setTilt(0)

kmlLookAt.setHeading(0)

' Times Square, New York, NY

kmlLookAt.setLatitude( 40.7594 )

kmlLookAt.setLongitude( -73.985 )

' setting new values

objActiveX.getView().setAbstractView(kmlLookAt)

'*******************放大 ******************

For i = 1 to 17

    kmlLookAt.setRange(kmlLookAt.getRange() * 0.5)

    objActiveX.getView().setAbstractView(kmlLookAt)

    Wait 1

Next