维基百科对 KML 文件解释:

KML全称:Keyhole Markup Language,是基于XML(eXtensible Markup Language,可扩展标记语言)语法标准的一种标记语言(markup language),采用标记结构,含有嵌套的元素和属性。由Google(谷歌)旗下的Keyhole公司发展并维护,用来表达地理标记。根据KML语言编写的文件则为KML文件,格式同样采用的XML文件格式,应用于Google地球相关软件中(Google Earth,Google Map, Google Maps for mobile…),用于显示地理数据(包括点、线、面、多边形,多面体以及模型…)。而现在很多GIS相关企业也追随Google开始采用此种格式进行地理数据的交换。

KML在 2008 年成为国际标准

KML在2008年4月14日被OGC(Open Geospatial Consortium, Inc.開放地理信息系統協會,或譯成開放式地理空間協會)宣布為開放地理資訊編碼標準(OGC KML, OpenGIS® KML Encoding Standard),而Google同時也在網站Blog上宣布不再控制KML標準,而移交給OGC去維護發展,而被採用的開放地理資訊編碼標準(OpenGIS KML 2.2 Encoding Standard)可以在這個網頁見到完整的手冊內容:http://www.opengeospatial.org/standards/kml/

地标

These are just some of the different kinds of placemarks with which you can mark your favorite places

表示地图上的点,只包含一个 <Point> 元素。在Google Earth中通常是黄色图钉。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark>
 <name>Simple placemark</name>
 <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>
 <Point>
 <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
 </Point>
 </Placemark>
</kml>

文件结构解释:

  • XML 表头
  • KML 命名空间声明
  • 包含以下元素的地标对象:
    • 用作地标标签的名称
    • 地标提示信息
    • 指定地标在地球表面的位置点-经度、纬度和高度(可选)

路径 Path

在 KML 中,路径是用 <LineString> 元素创建的。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Document>
 <name>Paths</name>
 <description>Examples of paths. Note that the tessellate tag is by default
 set to 0. If you want to create tessellated lines, they must be authored
 (or edited) directly in KML.</description> <Style id="yellowLineGreenPoly">
 <LineStyle>
 <color>7f00ffff</color>
 <width>4</width>
 </LineStyle>
 <PolyStyle>
 <color>7f00ff00</color>
 </PolyStyle>
 </Style> <Placemark>
 <name>Absolute Extruded</name>
 <description>Transparent green wall with yellow outlines</description>
 <styleUrl>#yellowLineGreenPoly</styleUrl>
 <LineString>
 <extrude>1</extrude>
 <tessellate>1</tessellate>
 <altitudeMode>absolute</altitudeMode>
 <coordinates> -112.2550785337791,36.07954952145647,2357
 -112.2549277039738,36.08117083492122,2357
 -112.2552505069063,36.08260761307279,2357
 -112.2564540158376,36.08395660588506,2357
 -112.2580238976449,36.08511401044813,2357
 -112.2595218489022,36.08584355239394,2357
 -112.2608216347552,36.08612634548589,2357
 -112.262073428656,36.08626019085147,2357
 -112.2633204928495,36.08621519860091,2357
 -112.2644963846444,36.08627897945274,2357
 -112.2656969554589,36.08649599090644,2357 </coordinates>
 </LineString> </Placemark>
 </Document> </kml>

请注意,该代码生成的其实只是高于地面的一条折线。<tessellate> 标签将该折线切成小段,而 <extrude> 标签将其向下延伸到地面。

多边形 Polygon

五角大楼的示例是通过绘制简单的内外壳,然后将它们向下凸出到地面生成的。代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark>
 <name>The Pentagon</name>
 <Polygon>
 <extrude>1</extrude>
 <altitudeMode>relativeToGround</altitudeMode>
 <outerBoundaryIs>
 <LinearRing>
 <coordinates>
 -77.05788457660967,38.87253259892824,100 
 -77.05465973756702,38.87291016281703,100 
 -77.05315536854791,38.87053267794386,100 
 -77.05552622493516,38.868757801256,100 
 -77.05844056290393,38.86996206506943,100 
 -77.05788457660967,38.87253259892824,100
 </coordinates>
 </LinearRing>
 </outerBoundaryIs>
 <innerBoundaryIs>
 <LinearRing>
 <coordinates>
 -77.05668055019126,38.87154239798456,100 
 -77.05542625960818,38.87167890344077,100 
 -77.05485125901024,38.87076535397792,100 
 -77.05577677433152,38.87008686581446,100 
 -77.05691162017543,38.87054446963351,100 
 -77.05668055019126,38.87154239798456,100
 </coordinates>
 </LinearRing>
 </innerBoundaryIs>
 </Polygon>
 </Placemark> </kml> 

参考: