看了Servlet-based Google Earth Tours感觉挺好,不过他这个不够简单,我来仿照KML官方网站的python例子,写个简单的小例子把文章的精髓拿出来.
快速体验:
1. 把下面的getest.kml和getest.py保存到本机。
2.把getest.py的第一行改成你的python解释器路径,并放到Apache的cgi-bin下。
3. 双击getest.kml会启动google earth,在Place面板里会出现Python Demo目录,选中它,当你移动屏幕的时候,你会发现屏幕中心一直会有一个placemark,这个placemark是python程序动态返回的。
原理介绍:
1. 先看看KML的<NetworkLink>标签的样子,比如getest.kml
< kml xmlns ="http://earth.google.com/kml/2.2" >
< Folder >
< name > Python Demo </ name >
< visibility > 0 </ visibility >
< open > 0 </ open >
< description > Liu Test </ description >
< NetworkLink >
< name > Random Placemark </ name >
< visibility > 0 </ visibility >
< open > 0 </ open >
< description > my discription </ description >
< refreshVisibility > 0 </ refreshVisibility >
< flyToView > 0 </ flyToView >
< Link >
< href > http://localhost/cgi-bin/getest.py </ href >
< refreshInterval > 2 </ refreshInterval >
< viewRefreshMode > onStop </ viewRefreshMode >
< viewRefreshTime > 1 </ viewRefreshTime >
</ Link >
</ NetworkLink >
</ Folder >
</ kml >
这里主要有两个地方:
一个是 <href>http://localhost/cgi-bin/getest.py</href> ,GE向这个URL要数据,并把BBOX以GET方式发送到该URL,比如http://localhost/cgi-bin/getest.py?bbox=121,36,122,37
另一个是<refreshInterval>2</refreshInterval>
<viewRefreshMode>onStop</viewRefreshMode>
<viewRefreshTime>1</viewRefreshTime>
这几个标签指定了GE发送数据的时间间隔和其他设定。
当我们在GE中打开这个KML文件的时候,GE就回向 http://localhost/cgi-bin/getest.py 要数据,我们就可以在getest.py中对该请求进行处理,并返回结果(KML)。
2. 第二步就是编写getest.py处理这个响应,最简单的是返回一个位于屏幕中心的地标,代码如下:
# replace the line above with our own python path
import cgi
url = cgi.FieldStorage()
bbox = url[ ' BBOX ' ].value
bbox = bbox.split( ' , ' )
west = float(bbox[0])
south = float(bbox[ 1 ])
east = float(bbox[ 2 ])
north = float(bbox[ 3 ])
center_lng = ((east - west) / 2 ) + west
center_lat = ((north - south) / 2 ) + south
kml = (
' <?xml version="1.0" encoding="UTF-8"?> '
' <kml xmlns="http://earth.google.com/kml/2.2"> '
' <Placemark> '
' <name>View-centered placemark</name> '
' <Point> '
' <coordinates>%.6f,%.6f</coordinates> '
' </Point> '
' </Placemark> '
' </kml> '
) % (center_lng, center_lat)
print ' Content-Type: application/vnd.google-earth.kml+xml '
print kml
我们也可以在getest.py中编写复杂的功能,比如从数据库中提取数据生成KML文件,然后返回客户端与GE的数据叠加。原理介绍,简单至上,就这么简单吧。
学习资料(摘自http://www.javaworld.com/javaworld/jw-11-2005/jw-1114-google.html):
Resources
- Download the source code that accompanies this article “Servlet-based Google Earth Tours”
http://www.javaworld.com/javaworld/jw-11-2005/google/jw-1114-google.zip - Cylindrical projection
http://mathworld.wolfram.com/CylindricalProjection.html - Google Earth API
http://code.google.com/apis.html#earth - Google Earth Community
http://bbs.keyhole.com/ubb/ubbthreads.php/Cat/0 - Google Earth download
http://earth.google.com/download-earth.html - Google Earth homepage
http://earth.google.com/ - Hibernate framework
http://www.hibernate.org/ - iBATIS framework
http://ibatis.apache.org/index.html - Jamon framework
http://www.jamon.org/Features.html - Microsoft Virtual Earth
http://virtualearth.msn.com/ - Nasa World Wind
http://worldwind.arc.nasa.gov/ - Google Earth KML Tutorial, where you'll find the algorithm for centering
http://www.keyhole.com/kml/kml_tut.html#tracking_point - For more articles on servlets, browse the Servlets section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-servlets-index.shtml - For more articles on how to put your Java skills to work, browse the Applied Java section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-applied-index.shtml