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>
-
值:
- 无. 父元素:
-
被以下元素包含:
<Document>
<Folder>
<NetworkLink>
子元素:
-
包含:
<Folder>
<name>
<description>
<LookAt>
<visibility>
<Placemark>
<GroundOverlay>
<ScreenOverlay>
<NetworkLink>
<Document>
<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>
<Icon>
<href>C:/GoogleEarth/example.jpg <href>
<Icon>
-
值:
-
图片路径.
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>
<IconStyle>
<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>
<PolyStyle>
<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>
子元素: 无.
本文深入探讨了KML文档的语法规则,包括XML标签的使用、文档结构、属性和各种KML标签的详细说明。
1596

被折叠的 条评论
为什么被折叠?



