以下小段代碼達到的效果是kml利用networklink不斷去執行伺服器上的phython代碼,動態改變點的經緯度座標。
首先在Internet資訊服務下預設網站下建立虛擬目錄,然後在該目錄下的“虛擬目錄”選項卡中建立應用程式,再點擊“配置”,進入“應用程式配置”,點擊添加(如),最後確定。
在該目錄的資源管理員下建立randomPlacemark.py檔案,代碼如下:
#!C:/Python25/python.exe#!以上更改為自己的python安裝路徑
import random
latitude = random.randrange(-90, 90)
longitude = random.randrange(-180, 180)
kml = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<kml xmlns="http://www.opengis.net/kml/2.2">\n'
'<Placemark>\n'
'<name>Random Placemark</name>\n'
'<Point>\n'
'<coordinates>%d,%d</coordinates>\n'
'</Point>\n'
'</Placemark>\n'
'</kml>'
) %(longitude, latitude)
print 'Content-Type: application/vnd.google-earth.kml+xml\n'
print kml
然後建立檔案test.kml
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Folder>
<name>Network Links</name>
<visibility>1</visibility>
<open>0</open>
<description>Network link example 1</description>
<NetworkLink>
<name>Random Placemark</name>
<visibility>1</visibility>
<refreshVisibility>1</refreshVisibility>
<open>0</open>
<description>A simple server-side script that generates a new random
placemark on each call</description>
<refreshVisibility>0</refreshVisibility>
<flyToView>1</flyToView>
<Link>
<href>http://computername/虛擬目錄名/randomPlacemark.py</href>
<refreshMode>onInterval</refreshMode>
<refreshInterval>5</refreshInterval>
<viewRefreshMode>onStart</viewRefreshMode>
<viewRefreshTime>4</viewRefreshTime>
</Link>
</NetworkLink>
</Folder>
</kml>