KML語法規則
和任何程序和腳本語言一樣,KML有一些必須遵循的語法規則.
文檔結構
一個KML文檔應該完全遵循KML格式,文檔也和基本的XML語法規則差不多,有以下幾點要特別注意的地方.
- XML標籤必須關閉
- XML標籤是大小寫敏感的
- 對於KML語法,首字母大寫的標籤是複合標籤,否則就是單一標籤,在實體和標籤介紹之中有更多相關信息.
- XML標籤必須正確嵌套
- XML文檔必須只有一個根標籤
- 對於KML文件,這意味着你可以使用<kml></kml>, <Document></Document> <Folder></Folder>甚至 <Placemark></Placemark>作爲根標籤
- 屬性必須用引號包圍起來
- CR/LF(回車符)被認爲是插入一個新行(在HTML描述之中,被轉化爲<br>)
- XML的註釋方法和HTML是一樣的
要了解更多信息,可以去查閱XML參考文檔或語法嚮導.如果你的KML文檔結構不符合XML文檔規範,將會在Google Earth中產生一個解析錯誤的對話框 (查看http://www.w3.org/XML/ 瞭解根多關於XML的信息.)
注意: XML Schema 驗證還沒有被Google Earth支持.
標記類型
有兩種基本的KML標記類型: 單一標籤和複合標籤.複合標籤的標籤名首字母是大寫的,而單一標籤都是小寫的,複合標籤能夠作爲其他標籤(單一標籤或複合標籤)的父元素, ,而單一標籤只能是其他複合標籤的子元素,而自身不能包含其他元素.
字符串
在KML文件中的任何字符串,例如 name 和 description標籤中的值,都可以是utf-8或者Unicode編碼的字符串.
KML標籤列表
這一節包含所有能用的KML標籤的參考,按字母順序排列,每一段列出了該標籤正確的大小寫格式,類型、值和層次.
<address>
- 值:
- 代表一個點所在位置或郵政編碼的字符串.
- 父元素:
- 被以下元素包含:
<Placemark> - 子元素:
- 無.
<altitudeMode>
- 值:
- clampedToGround、relativeToGround或absolute.
- 父元素:
- 被以下元素包含:
<Point>
<LineString>
<Polygon> - 子元素:
- 無.
<begin>
<begin>
<TimeInstant>
<timePosition>1876-08-01<timePosition>
<TimeInstant>
<begin>
- 值:
- 使用UTC(Coordinated Universal Time)格式的日期時間 .查看<TimeInstant>和<timePosition>瞭解更多關於時間格式的信息.
- 父元素:
- 被以下元素包含:
<TimePeriod> - 子元素:
- 包含:
<TimeInstant>
<color>
- 值:
- The standard range from 00000000 to ffffffff
- 父元素:
- 被以下元素包含:
<PolyStyle>
<LineStyle>
<IconStyle> - 子元素:
- 無.
<coordinates>
<Point>
<coordinates>-111.661,33.2212,0<coordinates>
<Point>
- 值:
- 由點的座標決定,經緯度的單位是度,而高度的單位是米(海拔).
注意:應該按照 經度,緯度,高度 這樣的格式和順序來設置,否則座標可能會不準確. - 父元素:
- Contained by any geometry.
- 子元素:
- 無.
<description>
支持純文本和HTML語法,如果其中包含有效的網址,則Google Earth會自動顯示爲一個連接T(例如http://www.google.com).而不需要使用任何標籤
如果你使用任何的HTML語法,就必須使用字符實體引用或者CDATA來防止HTML字符如><被當作XML解析,下面的例子說明了description標籤的用法:
<description>
<![CDATA[<a href="http://www.google.com.honeycomb.cs.cornell.edu:8888/">Google Search!</a>]]>
<description>
CDATA元素讓KML解析器忽略其內部的特殊字符.按以下方式使用該元素:
<![CDATA[ special characters here ]]>
如果你不喜歡使用CDATA元素,你也可以使用實體引用來替換所有的特殊字符.
- 值:
- 任意字符串
注意: description標籤只是支持HTML格式化,並不支持其他的網絡性能,比如腳本語言和Iframe. - 父元素:
- 被以下元素包含:
<Document>
<Folder>
<NetworkLink>
<GroundOverlay>
<ScreenOverlay>
<Placemark> - 子元素:
- 無.
<Document>
<Document>
<Style id="my_style">
<Icon>./images/my_image.png<Icon>
<Style>
. . . .
<Document>
- 值:
- 無.
- 父元素:
- 無.
- 子元素:
- Can contain the following tags:
<Folder>
<name>
<description>
<LookAt>
<visibility>
<Placemark>
<GroundOverlay>
<ScreenOverlay>
<NetworkLink>
<Document>
<drawOrder>
<drawOrder>0<drawOrder>
- 值:
- 從0到99的範圍.
- 父元素:
- 被以下元素包含:
<GroundOverlay>
<ScreenOverlay> - 子元素:
- 無.
<east>
- 值:
- 由圖片標記所描述的實際地理位置決定,經度和緯度以度數表示,而高度則以海拔高度(以米爲單位)來表示.
- 父元素:
- 被以下元素包含:
<LatLonBox> - 子元素:
- 無.
<end>
<end>
<TimeInstant>
<timePosition>2004-08-01<timePosition>
<TimeInstant>
<end>
- 值:
- 使用UTC(Coordinated Universal Time)格式的日期時間 .查看<TimeInstant>和<timePosition>瞭解更多關於時間格式的信息.
- 父元素:
- 被以下元素包含:
<TimePeriod> - 子元素:
- 包含:
<TimeInstant>
<extrude>
- 值:
- 以米爲單位的extrusion高度值.
- 父元素:
- 被以下元素包含:
<LineString>
<Polygon>
<Point> - 子元素:
- 無.
<Folder>
<Folder>
<name>Name of Folder<name>
<description>Descriptive text<description>
<Folder>
<name>SubFolder #1 Name<name>
<description>Descriptive text<description>
<Placemark>
[placemark data here ...]
<Placemark>
<Folder>
<Folder>
<name>SubFolder #2 Name<name>
<description>Descriptive text<description>
<Placemark>
[placemark data here ...]
<Placemark>
<Folder>
<Folder>
<geomColor>
<Placemark>
<Style ID="myIconStyleID">
<geomColor>ff640000<geomColor>
<Style>
<Point>
<coordinates>-122.072408, 37.412161, 6.936751<coordinates>
<Point>
<styleUrl>#myIconStyleID<styleUrl>
...
<Placemark>
注意:顏色被設置後,圖標的表面顏色會被<geomColor>的顏色值進行multiply(正片疊底)處理. 例如,如果圖表顏色是藍綠色,並且你設置<geomColor>爲綠黃色,最後顯示出來就將會是綠色. 注意這個正片疊底模式, RGB顏色值分別相乘(R*R, G*G, B*B).因此,紅色(1, 0, 0)和綠色(0, 1, 0) 最後會生成黑色,因爲(1*0, 0*1, 0*0)最後是(0, 0, 0).
- 值:
- 16進製表示的顏色,包括透明度的值,順序是alpha, blue, green, red (ABGR).
- 父元素:
- 被以下元素包含:
<Style> - 子元素:
- 無.
<GeometryCollection>
<GeometryCollection>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>.....
<coordinates>
<LinearRing>
<outerBoundaryIs>
<Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>.....
<coordinates>
<LinearRing>
<outerBoundaryIs>
<Polygon>
<GeometryCollection>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Placemark> - 子元素:
- 能夠包含任何幾何形狀,包括其它GeometryCollection.
<geomScale>
<Style id="myPlacemarkID">
<geomScale>2<geomScale>
<labelScale>1.08</labelScale>
<geomColor>ff08ceff<labelColor>
<Icon>
<href>root://icons/....<href>
<Icon>
<Style>
通常,這個標籤並不影響多邊形,除非多邊形使用outline模式,在這個模式下,只有多邊形的外輪廓的線有效果.
- 值:
- 代表縮放到的倍數的小數.例如,默認的圖標大小是1而geomScale設置爲.5,則設置後圖標大小便爲原來的一半同樣,如果geomScale設置爲2則將使圖表大小翻倍. 對於線段,這個只代表線寬,默認的線寬是1.
- 父元素:
- 被以下元素包含:
<Style> - 子元素:
- 無.
<GroundOverlay>
<GroundOverlay>
<visibility>0<visibility>
<Icon>
<href>C:/GoogleEarth/example.jpg<href>
<Icon>
<drawOrder>0<drawOrder>
<LatLonBox>
<rotation>36.9994<rotation>
<north>39.3082<north>
<south>38.5209<south>
<east>-95.1583<east>
<west>-96.3874<west>
<LatLonBox>
<GroundOverlay>
- 值:
- 無.
- 父元素:
- 任何有目錄功能的標籤.
- 子元素:
- 包含:
<drawOrder>
<Icon> (必需)
<LatLonBox> (必需)
<visibility>
<rotation>
<h>
- 值:
- 一個整數值,指定圖片上被提取區域的高度像素值.
- 父元素:
- 被以下元素包含:
<Icon> - 子元素: 無.
<heading>
- 值:
- 由觀測者(視點)方向決定.以度爲單位.
- 父元素:
- 被以下元素包含:
<LookAt> - 子元素:
- 無.
<href>
- 值:
- 圖片路徑.
C:/GoogleEarth/example.jpg
如果是網絡服務器上的圖片,則給出該圖片的URL地址:
http://www.keyhole.com/images/nav/navTopLogo.png
另外,如果要指定一個的圖片做圖標,你也可以實現從大圖片上截取一塊區域作爲圖標.而且這樣將會有更好的性能.
例如,你可能有一張圖片,由16個小圖片按4X4排列而成,你可以實現引用其中的一每小塊圖片作爲圖標,只要你指定該小塊圖片在整個圖片上的的以下四個座標值:
x、y 座標,小塊區域左上角的像素座標
w、h 座標,小塊區域的寬度和高度像素值
<Style id="Example">
<IconStyle>
<Icon>
<href>example.png<href>
<x>128<x>
<y>0<y>
<w>64<w>
<h>64<h>
<Icon>
<IconStyle>
<Style> - 父元素:
- 被以下元素包含:
<Icon> - 子元素:
- 無.
<Icon>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<GroundOverlay>
<ScreenOverlay>
<Style> - 子元素:
- 包含:
<href>
<x>
<y>
<w>
<h>
<innerBoundaryIs>
<Polygon>
<innerBoundaryIs>
<LinearRing>
<coordinates>
-88.306534, 30.227852, 0.000000 ...... -88.306534, 30.227852, 0.000000
<coordinates>
<LinearRing>
<innerBoundaryIs>
<Polygon>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Polygon> - 子元素:
- 無.
<key>
<StyleMap id="example_style">
<Pair>
<key>normal</key>
<styleUrl>#example_style_off<styleUrl>
<Pair>
<Pair>
<key>highlight</key>
<styleUrl>#example_style_on<styleUrl>
<Pair>
<StyleMap>
- 值:
- normal代表該標註的正常樣式,highlight代表該標註被選中情況下的樣式.
- 父元素:
- 被以下元素包含:
<Pair> - 子元素:
- 無.
<labelColor>
- 值:
- 默認值是ffffffff (白色,不透明).可以是16進制的符號並按照aabbggrr(alpha, blue, green, red)排列,其中,透明度是從0(透明)-255(不透明). 要了解更多請查看關於顏色的介紹文章.
- 父元素:
- 被以下元素包含:
<Style> - 子元素:
- 無.
<latitude>
- 值:
- 由視點座標決定,單位是度.
- 父元素:
- 被以下元素包含:
<LookAt> - 子元素:
- 無.
<LatLonBox>
<LatLonBox>
<rotation>36.9994<rotation>
<west>-96.3874<west>
<east>-95.1583<east>
<south>38.5209<south>
<north>39.3082<north>
<LatLonBox>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<GroundOverlay> - 子元素:
- 包含:
<east>
<west>
<north>
<south>
<rotation> (可選)
<LinearRing>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-88.306534, 30.227852, 0.000000 ...... -88.306534, 30.227852, 0.000000
<coordinates>
<LinearRing>
<outerBoundaryIs>
<Polygon>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Polygon> - 子元素:
- 包含:
<coordinates>
<LineString>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Placemark>
<GeometryCollection> - 子元素:
- 包含:
<coordinates>
<longitude>
- 值:
- 由視點座標決定,單位是度.
- 父元素:
- 被以下元素包含:
<LookAt> - 子元素:
- 無.
<LookAt>
<LookAt>
<heading>-0.00895499<heading>
<tilt>39.4365<tilt>
<range>214.17<range>
<latitude>37.3895<latitude>
<longitude>-122.086<longitude>
<LookAt>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Folder>
<Document>
<Placemark>
<GroundOverlay> - 子元素:
- 包含:
<heading>
<latitude>
<longitude>
<range>
<name>
- 值:
- 任意字符串.
- 父元素:
- 被以下元素包含:
<Folder>
<Document>
<GroundOverlay>
<Placemark>
<ScreenOverlay>
<Schema> - 子元素:
- 無.
<NetworkLink>
<NetworkLink>
<refreshVisibility>0<refreshVisibility>
<refreshInterval>121<refreshInterval>
<Url>
<href>//myServer/Googleearth/example.kmlM.<href>
<Url>
<visibility>1<visibility>
<name>TestNetworkLink<name>
<NetworkLink>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Folder>
<Document> - 子元素:
- 包含:
<refreshVisibility>
<refreshInterval>
<<Url>>
<name>
<visibility>
<north>
- 值:
- 由圖片標記所描述的實際地理位置決定,經度和緯度以度數表示.
- 父元素:
- 被以下元素包含:
<LatLonBox> - 子元素:
- 無.
<ObjArrayField>
<ObjField>
<open>
- 值:
- 在載入時,如果值爲1,則該目錄默認會被展開,如果值爲1,則默認是閉合的,如果目錄包含大量的標註,最好確保目錄默認閉合狀態.
- 父元素:
- 被以下元素包含:
<Folder> - 子元素:
- None.
<outerBoundaryIs>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-88.306534, 30.227852, 0.000000 ...... -88.306534, 30.227852, 0.000000
<coordinates>
<LinearRing>
<outerBoundaryIs>
<Polygon>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Polygon> - 子元素:
- 包含:
<LinearRing>
<overlayXY>
- 值:
- x和y能夠用以下方式指定:
將圖片顯示在屏幕的中間:
<overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
將圖片顯示在屏幕的左上角:
<overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
<screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
將圖片顯示在屏幕的右邊:
<overlayXY x="1" y="1" xunits="fraction" yunits="fraction"/>
<screenXY x="1" y="1" xunits="fraction" yunits="fraction"/> - 父元素:
- 被以下元素包含:
<ScreenOverlay> - 子元素:
- 無.
<Pair>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<StyleMap> - 子元素:
- Must contain the following:
<key>
<styleUrl>
<parent>
<Schema>
<name>High School<name>
<parent>Placemark<parent>
<SimpleField>
<name>Address<name>
<type>wstring<type>
<SimpleField>
<SimpleField>
<name>Average SAT score<name>
<type>int<type>
<SimpleField>
<Schema>
- 值:
- 一個已經存在的KML基本類型,查看KML基本類型獲得更多信息.
- 父元素:
- 被以下元素包含:
<Schema> - 子元素:
- 無.
<Placemark>
經度: 在-180和180之間
緯度:在-90和90
高度:以米爲單位的海拔高度(WGS84). 在地表面下面的點將會自動顯示在地表面以上.
另外,你可以爲placemark定義一系列其他的元素如視點、名稱和描述.
<Placemark>
<name>Queen Creek<name>
<LookAt>
<longitude>-111.634<longitude>
<latitude>33.2407<latitude>
<range>5571.15<range>
<tilt>-0.0129749<tilt>
<heading>-0.0651017<heading>
<LookAt>
<visibility>1<visibility>
<styleUrl>root://styleMaps#default?iconId=0x300<styleUrl>
<Point>
<coordinates>-111.634,33.2486,413.037<coordinates>
<Point>
<Placemark>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Folder>
<Document> - 子元素:
- 包含:
<description>
<name>
<Point>
<styleUrl>
<LookAt>
<visibility>
<Point>
<Point>
<coordinates>-111.965,33.3217,355.27 <coordinates>
<Point>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Placemark>
<GeometryCollection> - 子元素:
- 包含:
<coordinates>
<Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-88.306534, 30.227852, 0.000000 ...... -88.306534, 30.227852, 0.000000
<coordinates>
<LinearRing>
<outerBoundaryIs>
<Polygon>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Placemark>
<GeometryCollection> - 子元素:
- 包含:
<outerBoundaryIs>
<innerBoundaryIs>
<range>
- 值:
- 由視點的位置決定.
- 父元素:
- 只能被<LookAt>包含.
- 子元素: 無.
<refreshInterval>
<NetworkLink>
<refreshVisibility>0<refreshVisibility>
<refreshInterval>121<refreshInterval>
<<Url>>//myServer/GoogleEarth/example.kml</<Url>>
<visibility>1<visibility>
<name>TestNetworkLink<name>
<NetworkLink>
- 值:
- 以秒爲單位.如果該值大於0,則該項每n秒刷新一次,如果該值小於0,則該項根本不會被加載,如果該值等於0,則該項會被加載一次而不會再被刷新.
- 父元素:
- 被以下元素包含:
<NetworkLink>
<GroundOverlay>
<ScreenOverlay> - 子元素:
- 無.
<refreshVisibility>
- 值:
- 默認值是0,即忽略KML文檔中的默認可見性設置而使所有標註的可見性不變 . 更改爲1既可以在每次網絡KML文件被刷新之後重新根據KML文件內容設置標註的可見性. 例如,假設有一項在KML文件之中可見性設置爲0,而且refreshVisibility設置爲1,當文件第一次加載到Google Earth之中的時候,該項是不顯示的如果用戶通過點擊該項的複選框而已經使該項顯示出來,這種情況下該網絡KML文件被刷新之後,該項又會重新變成不顯示.
翻譯到這裏,我只好感嘆又學了一招,我剛纔試了一下,果然不錯,非常有用。 - 父元素:
- 被以下元素包含:
<NetworkLink> - 子元素:
- 無.
<rotation>
- 值:
- 使用-180~+180的角度值來指定從0度開始的順時針旋轉角.0爲默認值(不旋轉)
- 父元素:
- 被以下元素包含:
<GroundOverlay>
<ScreenOverlay>
<LatLonBox> - 子元素:
- 無.
<Schema>
- 值:
- 無.
- 父元素:
- 無.
- 子元素:
- 包含:
<name>
<parent>
<ObjField>
<ObjArrayField>
<SimpleField>
<SimpleArrayField>
<ScreenOverlay>
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<ScreenOverlay id="khScreenOverlay756">
<description>This screen overlay uses fractional positioning to put the image in the exact center of the screen<description>
<name>Simple crosshairs<name>
<visibility>0<visibility>
<Icon>
<href>http://myserver/myimage.jpg<href>
<Icon>
<overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<rotationXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<size x="0" y="0" xunits="pixels" yunits="pixels"/>
<ScreenOverlay>
</kml>
- 值:
- 無.
- 父元素:
- 被以下元素包含:
<Folder>
<Document> - 子元素:
- 包含:
<drawOrder>
<Icon> (必需)
<visibility>
<rotation>
<size>
<screenXY>
<overlayXY>
<screenXY>
- 值:
- x和y能夠用以下方式指定:
將圖片顯示在屏幕的中間:
<overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
將圖片顯示在屏幕的左上角:
<overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
<screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
將圖片顯示在屏幕的右邊:
<overlayXY x="1" y="1" xunits="fraction" yunits="fraction"/>
<screenXY x="1" y="1" xunits="fraction" yunits="fraction"/> - 父元素:
- 被以下元素包含:
<ScreenOverlay> - 子元素:
- 無.
<SimpleArrayField>
<SimpleField>
<SimpleField>
<name>height<name>
<type>int<type>
<SimpleField>
<size>
- 值:
- 用法如下.
使用圖片本身的大小和縱橫比,應將值設成0:
<size x="0" y="0" xunits="fraction" yunits="fraction"/>
使圖片寬度不變,高度變爲原來的80%:
<size x="0" y="0.2" xunits="fraction" yunits="fraction"/>
強制圖片大小爲100*500:
<size x="100" y="500" xunits="pixels" yunits="pixels"/> - 父元素:
- 被以下元素包含:
<ScreenOverlay> - 子元素:
- 無.
<south>
- 值:
- 由圖片標記所描述的實際地理位置決定,經度和緯度以度數表示
- 父元素:
- 被以下元素包含:
<LatLonBox> - 子元素:
- 無.
<snippet>
- 值:
- 任意值
- 父元素:
- 被以下元素包含:
<Placemark> - 子元素: 無.