基于Google Earth客户端版本3.0 的KML 版本2.0
本文来源于Google由Step1翻译并整理,若有任何疑问,可到评论区留言
KML全称是Keyhole Markup Language KML,是一个基于XML语法和文件格式的文件,用来描述和保存地理信息如点、线、图片、折线并在Google Earth客户端之中显示, (KML以前的版本能够被Google Earth读取并保存为KML 2.0) KML 2.0提供以下功能:
KML被Google Earth viewer显示的过程和HTML网页被浏览器处理差不多,而且和HTML一样,KML也使用一种基于标签(名称和属性)的语法格式来描述地里标注信息,可以说,Google Earth viewer是一个KML文件浏览器.
KML 1.0是由原先的Keyhole客户端进行读写的文件格式,Google Earth完全支持KML 1.0文件,KML 2.0 是KML 1.0的升级版本,主要的更新如下:
Google Earth中的所有标注都是通过KML来描述的,KML有以下撰写方式:
对于希望创建通过E-mail或者网页来发布的不动态的KML文件的用户来说,Google Earth 客户端就是一个很好的KML撰写工具,在Google Earth客户端中简单的按照您自己的要求组织您需要的数据并保存成一个独立的文件即可, 在开发人员想要测试复杂的KML文档结构的时候,这个功能也同样有用, Google Earth客户端能够创建和保存几乎所有的KML标签。
你可以使用一个文本编辑器来创建简单的KML文件,或者测试一个KML文件的结构和语法的有效性。
TIP:
想要快速的检查KML语法,在Google Earth中右键点击一个地名标注或者图片标注,然后在菜单中选择"Copy",
然后你可以打开一个简单的文本文档然后将KML内容粘贴到其中,就可以方便的查看Google Earth撰写的KML内容了
您能够使用一种用来动态生成网页内容的编辑平台来生成KML地标文件,只要能够生成XML文件,就可以同样生成KML文件。
下面是一个KML文件范例:
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
<description><![CDATA[<a href="http://www.google.com.honeycomb.cs.cornell.edu:8888/">Google Search!</a>]]></description>
<name>Google Headquarters</name>
<LookAt>
<longitude>-122.0839</longitude>
<latitude>37.4219</latitude>
<range>540.68</range>
<tilt>0</tilt>
<heading>3</heading>
</LookAt>
<Point>
<coordinates>-122.0839,37.4219,0</coordinates>
</Point>
</Placemark>
</kml>
这个文件里有一个<kml>标签,包含一个被命名为Google Headquarters的<Placemark>标签, 当这个文件被Google Earth客户端读取的时候,<Placemark>将会以默认的图标和在<Point>中指定的经纬度处显示, <Placemark>的<LookAt>子标签指定当在Google Earth中显示这个标注的时候"照相机"视点.
你可以复制该范例内容并保存成扩展名为.kml的文本文件,在Google Earth之中打开查看效果
本文档的剩下部分还包含以下内容:
Google Earth 客户端支持了大量的显示标签用来创建和保存成KML文档。 如果你喜欢,你可以先用文本编辑器修改KML文档,然后在Google Earth客户端中打开看显示的效果, 这和你修改一个HTML文档并在浏览器之中观看效果是一样的。 对于更高级的程序,你可以使用CGI脚本生成KML文件,这一节讲解在Google Earth客户端之中支持的KML基本标签。
在本节中描述的所有标注(目录folders,地名标注placemarks,图片标注overlays 和 网络连接)都包含以下元素.
KML语法使用<name></name>标签来指定目录(floder)、地名标注(placemark)、图片标注(overlay)或其他标记的名称, 以便在Google Earth中进行区别,下面的例子显示了<name>标签在<Folder>标签上的应用:
<Folder> <name>My House</name> . . . </Folder>
使用描述元素给标注附加更多的关于该标注的信息,Google Earth自动识别其中的网址并显示为一个链接,并不需要任何HTML标记, 不过你也可以再需要时使用HTML格式化文本的显示,甚至还可以使用图片。
为了在描述之中使用HTML标签,您需要使用CDATA XML 标签来避免XML对HTML字符的解析,或者您也可以对特定的字符进行实体引用
注意:在第一个测试版中在<img src="">标签中部允许包含GIF图片,这个错误将在下一版本中修正,不过现在您可能还是只能使用JPG, PNG or BMP这几种格式。
<visibility>用来设置标注在第一次被Google Earth打开时的默认显示属性. Visibility对所有的标注都有效。 不过visibility在目录之中使用的时候存在特殊情况,一个标记只有在它和它所有的上级标签都设成true(1)的时候才会显示。
你可以定义该标注在用户打开KML文件的时候是否显示,在文件打开完成之后是否显示该标注完全取决于用户的操作。
<LookAt>元素用来指定对给定点的特定视角,Google Earth能够为地名标注(placemarks),目录(folders),图片标注(overlays)和其他标注设置视角, 当一个标注存在指定的<LookAt>标签时,双击该标注将使Google Earth平滑移动到指定视角。
值得注意的是,视角和标注的经纬度没有任何关系,例如,一个地名标注可能在一大片陆地的中央,而该标注对应的视角观察者却朝向陆地的左边边界。
实际上,一个标注的视点可能被设置到甚至根本看不到该标注本身的位置,因此当你为一个地名标注设置一个视点的时候, 不要认为它会自动的让该标注出现在Google Earth视图之中。 这样可能在你想为一个特定的标注使用一个特定的,不受限制的视角时非常有用,可有时候也会带来麻烦,因此,请谨慎使用本功能。 当一个标记没有<LookAt>标签时,双击该标签将使Google Earth缩放当前视图到包含该标注点
你可以使用包含地址字符串的<Point>标签来代替指定经纬度,例如<address>1600 Amphitherter Pkwy, Mountain View, CA</address> 这样将使用Google Earth的功能来解析这个地址(请注意这个功能现在只支持美国地址)
<snippet>标签允许您插入多行的描述文本,当用户选中该标签的时候,这些文本将显示在窗口的底部。
Placemarks are the primary means of marking a location on the earth.A plcaemark may contain a single point, linestring, polygon, or arbitrary collections of these features. User-defined styles may be created to control the appearance of each of these elements.
With KML, you can customize your placemark information in a variety of ways. In addition to the common elements, the placemark element uses a style reference (<styleUrl>) to specify the desired style for that placemark.
For example, you can use any image-such as your company logo-as the icon for a placemark by using the style reference to point to the desired icon. In addition, the style can reference a style map in order to display a different style when the user mouses over a placemark. In this way, you can have both a normal and a highlighted state for placemark icons. Finally, you can also use style references to set scale and color for geometry as well as point labels.
You can also specify a <Style> within a <Placemark>. This style is known as the Placemark's local style. Any fields that are specified in the local style will override the corresponding fields in the referenced style (i.e., any style referenced through <styleUrl>). Minimal use of local styles is recommended for best efficiency.
The following types of geometry can be expressed in the Google Earth client viewer:
Points may be mapped on the earth's surface as either icons or labels, or both. Placemarked points may be richly annotated within the Google Earth client, and may be positioned three-dimensionally. Points positioned at altitude may also be extruded, creating a line that tethers the placemark to the ground.
This includes lines and linestrings (polylines), but not road data. As with points, lines may be positioned at altitude, and extruded to form solid walls.
Two dimensional polygons may be created in the Google Earth client. As with lines, polygons may be positioned at altitude and extruded to form three-dimensional objects. Using a combination of points, lines, and polygons, it is easy to create rich, visually arresting displays of your data.
Lines and polygons may be grouped into collections.
Coordinate values for geometry must be entered in degrees (longitude, latitude) and meters (elevation above sea level). KML uses the <coordinates> tag to describe this feature. See the entry for <coordinates> for more information.
Important: The required geographic coordinates specification is Longitude, Latitude, and Altitude, in that order. If you don't follow this method, your coordinate specifications will be misinterpreted.
In the Google Earth client it is possible to create three-dimensional shapes by extruding two-dimensional objects. See the entry for <extrude> for more information.
Most uses of KML employ folders to organize placemarks, screen and ground overlays, and other folders. Use folders to structure collections of features and overlay groups, andto provide a unified view for a group of placemarks and/or overlays. In addition to the common elements, folders may have a number of extra properties:
A switch (0 or 1) that controls whether or not the folder will be in an expanded or collapsed state when it is loaded.
You can use a folder to contain related placemarks, overlays, and other folders. By providing folders with a name and LookAt values, you can group information in way to make it easily identifiable and manageable. For example, you can use a folder to associate a placemark with a related overlay image file for a multi-layered view of a particular region.
You can create a hierarchical structure in a manner similar to the Microsoft Windows folder view, so that folders can be expanded or collapsed to reveal or hide content as needed. Do this in the Google Earth client by dragging and dropping folders on top of other folders in order to nest them. In KML, <Folder></Folder> tags simply nest within each other, in the same way that <table></table> tags can be nested in HTML.
A document is a type of folder that can also contain the <Styles> and <Schemas> tags in addition to features such as placemarks, overlays and folders. Use documents to group styles and schemas for a collection. Documents are also often root-level containers of KML hierarchies.
Note: When you need to use styles or schema for your data, you must also use the <Document> element to contain these other elements. Use styles when you want to add custom icons and color and/or size information to geometry (see the entry for <Styles>). Use schemas when your data collection contains features that are not native to KML. With schemas, you can define a feature and its associated meta-data so that it is correctly displayed in the Google Earth client.
Image overlays can be described in KML for adding image-based meta-data to your GIS data in the Google Earth client. There are two types of overlays:
Use ground overlays for information-rich site display such as phased plans or weather maps. Ground overlays are images that are fixed to the planet surface.
A screen overlay is a "floating" image independent of the camera position and is fixed to the screen. Examples of screen overlays might include displaying a legend of key for the data, or perhaps copyright messages.
When using overlapping overlays, you can set the draw order to determine the overlay stacking order. In this way, for example, overlay images with black boundary edges or masked boundaries can be assigned a lower number than adjacent overlays so as not to obscure useful information.
The <Icon> tag references the overlay image file either locally or remotely, such as on your corporate web server. Remote users do not have to have a local copy of an overlay image if the image is supplied remotely. In this way, you can provide real-time updates of overlay data, such as weather, or development progress of site plans. The icon tag also contains child elements such as <refreshInterval>, which may be used to control automatic updates of remotely-served images (webcams, for example).
You can set the rotation of the overlay image. The image can be rotated up to 180 degrees in a clockwise (positive) or counter-clockwise (negative) rotation from north.
In KML, ground overlay tags can be placed inside any folder or subfolder in your KML document. In addition to the common elements, ground overlays support the following elements:
Four coordinates set the edges of the overlay. These coordinates set the North-South (latitude), East-West (longitude) positions of the overlay boundaries. In addition, you can indicate the overlay's rotation, which is about the center of the overlay. In the Google Earth client viewer, you can drag and rotate the overlay in order to set the coordinates, or you can enter in your own values.
Use screen overlays to display information regardless of the camera position, such as heads up display, co-branding, or other types of dashboard display. As with ground overlays, you can define multiple screen overlays and associate them with a folder. You can define the position as an absolute coordinate position in the viewer, or as a percentage of the viewer size. You can also set the color of a screen overlay.
Note: Screen overlays can currently only be authored in KML using a text editor, not in the Google Earth client.
Screen overlays have the following features:
Two mechanisms for screen positioning provide flexibility in different presentation formats. You can determine the position of the screen image using X/Y coordinates either with exact pixels, or as a fraction of the overall screen. See the entries for <overlayXY>, <screenXY> and <size> for more information.
Use the <size> tag to indicate the width and height of the overlay image based on the screen size. For more information, see the entry for <size>.
Time can be attached to all features in KML. Currently, time cannot be authored in the Google Earth client itself, but must be authored directly in KML. When the time feature is enabled (feature only available in the Google Earth EC client) all time-stamped features in a KML document can be made visible or hidden according to a number of adjustable parameters in the Google Earth EC client time user interface. For example, polygons representing each state of the United States can have a time value applied to represent the year the state joined the Union. Playback can be set in the Google Earth client to visually display the addition of each state.
For more information, see the entries for <timePosition>, <TimeInstant>, and <TimePeriod>.
Similar to styles in HTML, styles can be employed in KML to define the appearance of geometry, icons, and labels in the viewer. Styles can be used for the following effects:
You can use a <styleUrl> tag to reference a style selector. A style selector selects a specific style from a group of styles, and may be used to apply that style to any piece of geometry. There are three style selector types in KML:
A <Style> tag is used to indicate drawing style for icons, labels, lines and polygons. A <Style> tag is a style selector that selects itself. See the entry for <Style> for more information.
A <StyleMap> tag is used to provide a second level of style referencing for such features as placemarks. It selects a style based on the placemark highlight state. See the entry for <StyleMap> for more information.
A <StyleBlinker> tag can be used to reference custom icons so that they blink in the viewer. It selects a style based on time. The frequency and duration of the blinking is customizable.
Important: All style selectors must have an "id", the XML attribute, in order to be accessible.
The following example shows how it is possible to implement a simple "rollover" icon change. Two style ids are defined-normal and highlighted-which are then mapped to a styleUrl. The placemark has no local style, but instead is referenced to the global style.
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<name>Simple Icon Rollover</name>
<open>1</open>
<Style id="normalPlacemark">
<IconStyle>
<Icon>
<href>C:/green_icon.jpg</href>
</Icon>
</IconStyle>
</Style>
<Style id="highlightPlacemark">
<IconStyle>
<Icon>
<href>C:/red_icon.jpg</href>
</Icon>
</IconStyle>
</Style>
<StyleMap id="exampleStyleMap">
<Pair>
<key>normal</key>
<styleUrl>#normalPlacemark</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#highlightPlacemark</styleUrl>
</Pair>
</StyleMap>
<Placemark>
<name>Example Placemark</name>
<styleUrl>#exampleStyleMap</styleUrl>
<Point>
<coordinates>-122.0856545755255,37.42243077405461,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
For elements in your KML that are not native to the default KML schema, you can define your own schema as a set of named and typed XML elements. For example, you can specify how KML should interpret imported files such as from a GPS tracker. A schema requires the following:
The KML entity that the new schema will inherit from. For example, if you want the data in your KML document to inherit the elements of a placemark, you would set Placemark as the parent. See the entry for <parent> for more information.
As with all other features, the name element identifies the schema. See the entry for <name> for more information.
The field declaration specifies the individual fields in your KML data and defines their name and type. The following example illustrates:
<Schema>
<name>States</name>
<parent>Placemark</parent>
<SimpleField>
<name>FIPS</name>
<type>wstring</type>
</SimpleField>
<SimpleField>
<name>STATE</name>
<type>wstring</type>
</SimpleField>
</Schema>
Here, the schema inherits all the attributes of the Placemark element. It defines three simple fields -- FIPS, STATE, and NAME -- all of which are wide string fields (unicode UCS2 encoding). Subsequent use of those fields in the KML will be correctly interpreted by the Google Earth client as elements similar in structure to Placemark elements.
Use network links to quickly and easily share KML files among multiple users by sharing a link to the data rather than distributing the data itself. In addition to the common features, a network link has the following features:
Containes the information associated with the linked KML file. Network links can reference the KML file either on remote servers (http://..) or on local networks (\myServer\..). The Url tag is a parent to several child tags which control various aspects of the link's behavior. These are:
网络文件的路径
指定网络文件的刷新间隔,如果该值大于0,网址每隔n秒刷新一回,如果小于0,网址则不会被加载,如果等于0,则会只下载一次. 查看<refreshinterval>标签获取更多信息.
指定在Google Earth中的项变化时是否重置该项的可见性.例如,假设有一项在KML文件之中可见性设置为0,而且refreshVisibility设置为1,当文件第一次加载到Google Earth之中的时候,该项是不显示的如果用户通过点击该项的复选框而已经使该项显示出来,这种情况下该网络KML文件被刷新之后,该项又会重新变成不显示. 查看<refreshvisibility>标签获取更多信息.
网络连接有三种动态加载方式:用户手动请求、文件被加载时,和周期性的刷新, 查看<refreshmode>标签获取更多信息.
Google Earth能够向网络服务器发送基于视图的查询请求,这种请求可以是周期性(根据视图刷新时间的设置)的, 也可以是当视图停顿一段时间后. 一旦触发后,Google Earth向服务器发送一个包含当前视图坐标的请求,服务器根据坐标返回相应的信息内容, 查看<viewrefreshmode>标签获取更多信息.
和刷新间隔差不多,但是该项的值控制试图坐标的值每隔多长时间发送到服务器,查看<viewRefreshTime>标签获取更多信息
注意: 当使用本地网络路径的时候,路径之中的分隔符应该使用(//)而不是(\).
这一节包含KML标签的语法结构和参考信息.
和任何程序和脚本语言一样,KML有一些必须遵循的语法规则.
一个KML文档应该完全遵循KML格式,文档也和基本的XML语法规则差不多,有以下几点要特别注意的地方.
要了解更多信息,可以去查阅XML参考文档或语法向导.如果你的KML文档结构不符合XML文档规范,将会在Google Earth中产生一个解析错误的对话框 (查看http://www.w3.org/XML/ 了解根多关于XML的信息.)
注意: XML Schema 验证还没有被Google Earth支持.
有两种基本的KML标记类型: 单一标签和复合标签.复合标签的标签名首字母是大写的,而单一标签都是小写的,复合标签能够作为其他标签(单一标签或复合标签)的父元素, ,而单一标签只能是其他复合标签的子元素,而自身不能包含其他元素.
在KML文件中的任何字符串,例如
这一节包含所有能用的KML标签的参考,按字母顺序排列,每一段列出了该标签正确的大小写格式,类型、值和层次.