zoukankan      html  css  js  c++  java
  • KML 入门

    KML向导

         KML是一种在Earth Browser(比如Google Earth,Google MapsGoogle Maps for mobile)用于显示地理数据的文件格式。KML用使用一种可以嵌套元素和标记的结构并且基于XML标准。所有的这些标签都是大小写敏感的并且必须精确的出现在KML2.2Reference .这个参考手册指出哪些标记是可选的。在一个给定的元素中,标记必须按照Reference显示的顺序出现。

         如果你是一个KML新手,可以阅读这篇文档并且使用这些例子来开始学习KML文件基本结构。第一部分描述了使用Google Earth软件来创建的特性。这些特征包括placemarks,descripttions,ground overlays,pathspolygons。第二部分描述了使用一个text editor来创建的特性。当一个文件存储使用kml或者kmz扩展名,google earth浏览器知道怎样来显示它。

         目录

             基本的KML 文档

                 Placemarks

                 Descriptive HTML in Placemarks

                 Ground Overlays

                 Paths

                 Polygons

             高级的KML文档

                 对于Geometrystyles

                 对于高亮的iconsstyles

                 对于屏幕的覆盖图层

                 网络连接

             KML MIME Types

         一、基本KML文档

             KML最简单的文档是那些可以直接在Google Earth发布的。也就是说,你不必在一个text editor中编辑或创建任何KML,像这些PlacemarksDescriptive HTML in PlacemarksGround OverlaysPathsPolygons元素可以直接在Google Earth中创建。

             1.1 Placemarks

                 一个Placemarkgoogle earth中最普遍的特征。他标记真地球上表面的位置,使用一个黄色的图钉作为图标。最简单的placemark仅包括一个point元素,用于指定placemark的位置。你可以指定这个placemark的名字和图标,你也可以增加其他的几何元素在上面。

                Google Earth 打开KML Samples文件并且扩展placemarks子文件夹,这个文件夹包括三种placemark类型:simplefloatingextruded.对于KML code的一个简单的placemark如下:  

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://earth.google.com/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文件中第一行。在这行之前不能出现任何空格和字符。

    一个KML名空间声明。这是每个KML2.2文件的第2

    一个Placemark对象包含下列元素:

      • a name用于Placemark的文本标签
      • a description用于附在placemark的注释信息
      • a point用于指定地球表面的placemark的位置(经度,纬度和可选的海拔)

    如果你想知道这个Placemark在哪里,他正是google41号建筑,在那里开发的google earth.

             google earth中用户通常认为placemark是一个带有point元素的placemark的元素。一个point placemarkgoogle earth3d viewer用于绘制一个icon 和标签。默认,这个图标是大家熟悉的黄色图钉。在KML中,一个placemark可以包含一个或者多个几何元素,如linestring,polygon,model.但是一个带有pointplacemark能有一个icon和标签。这个点用于放置icon,但是本身这个点没有图形表现。

    1.2、在placemarks中放置具有描述性的HTML

           KML Samples中有一个你可以使用placemark text来处理任何事情的例子。你可以增加链接,字体颜色,样式和颜色还有指定文字对齐方式和表格。如果你想要看这些代码,可以将”Descriptive HTML”placemark 的例子粘贴到文本编辑器中。

    l         Google Earth中的自动链接标记(4.0和之后的发布版本)

    Google Earth4.0有一个自动链接的特性,这个特性用于自动将像www.google.com这样的文本转换成用户可以点击的链接。在<description>,<Snippet><BallonStyle><text>的标签中的文字全都自动转换成标准的HTTP链接。你不必自己添加<a href=…>标签。

    l         使用CDATA元素

    如果你想在在一个<description>标记写标准的HTML,你可以在里面添加CDATA标签。如果你不这么做,尖括号需要用实体字符来写以防止google earth不正确的转换政协HTML.例如,符号>别写成&gt;符号<别写成&lt;这是一个标准的xml特性并不是指对Google Earth

    比较在HTML标记使用CDATA标签和不使用CDATA的区别。首先,这儿是一段带有CDATA标签的<description>

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/kml/2.2">
     <Document>
        <Placemark>
          <name>CDATA example</name>
          <description>
            <![CDATA[
              <h1>CDATA Tags are useful!</h1>
              <p><font color="red">Text is <i>more readable</i> and 
              <b>easier to write</b> when you can avoid using entity 
              references.</font></p>
            ]]>
          </description>
          <Point>
            <coordinates>102.595626,14.996729</coordinates>
          </Point>
        </Placemark>
     </Document>
    </kml>

    这儿是一段没有CDATA标签的<description>.所以特殊字符必须使用实体字符。

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/kml/2.2">
     <Document>
        <Placemark>
          <name>Entity references example</name>
          <description>
                    &lt;h1&gt;Entity references are hard to type!&lt;/h1&gt;
                    &lt;p&gt;&lt;font color="green"&gt;Text is 
              &lt;i&gt;more readable&lt;/i&gt; 
              and &lt;b&gt;easier to write&lt;/b&gt; 
              when you can avoid using entity references.&lt;/font&gt;&lt;/p&gt;
          </description>
          <Point>
            <coordinates>102.594411,14.998518</coordinates>
          </Point>
        </Placemark>
     </Document>
    </kml>

    1.3Ground Overlay

           ground overlay可以使你将一个图片覆盖在Earth的地表上。这个<Icon>元素用于包含这个jpg文件的链接。这个在KML Samples文件的ground overlay例子,用于显示在2001Mount Etna 火山爆发的

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/kml/2.2">
     <Folder>

        <name>Ground Overlays</name>

        <description>Examples of ground overlays</description>

        <GroundOverlay>

          <name>Large-scale overlay on terrain</name>

          <description>Overlay shows Mount Etna erupting
              on July 13th, 2001.</description>

          <Icon>

            <href>http://code.google.com/apis/kml/documentation/etna.jpg</href>

          </Icon>

          <LatLonBox>

            <north>37.91904192681665</north>

            <south>37.46543388598137</south>

            <east>15.35832653742206</east>

            <west>14.60128369746704</west>

            <rotation>-0.1556640799496235</rotation>

          </LatLonBox>

        </GroundOverlay>

     </Folder>
    </kml>

    注意这个文件使用第一个例子中的同样的两行:XML头和KML名空间按声明。

    这个例子使用Folder来组织和标记它的内容。当你装载一个KML Samples文件到Googel Earth中注意在Places PanelFolder是怎么显示的。

           一个gournd overlay的位置使用<LatLonBox>标签控制。边界值为南北的纬度和东西的经度。此外,对于给定的图像的y-asixrotation 值不能和grid north 冲突。这个例子使用一个JPEG图像覆盖。Google Earth也支持BMP,GIF,TIFF,TGAPNG格式。

    1.4Path

           Google Earth中可以创建许多不同的path类型,在你的数据中赋有创新型是非常容易的。在KML中,a path是使用<LineString>元素来创建。看一下在Path folder中的”Absolute Starndard”例子,并且你能看到下列代码能产生多少个shape.

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/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>标签扩展地上的线。
    1.5Polygons

          你可以使用Polygons来创建简单的buildings和其他的形状。查看KML Samples文件的Polygon文件夹下的例子。

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/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>

    二、高级KML文档

           这一部分讲述一些不需使用一个文本编辑器来编辑的KML元素,像对于一个geometry,对于placemark高亮的icons和屏幕覆盖层的共享styles。手动的编写KML比使用google earth来创建和修改features更加高级,但是用一点实践,大多数用户习惯于编写KML文件来增加效果。

          2.1、对于几何实体的Styles

           一旦你使用Google Eeath来创建features并且检查Google Earth产生的code,你就会注意到样式对于你的数据怎样显示是多么重要的一部分。用户会想学怎么样定义自己的样式。

    如果你在一个KML文档的开头定义一个样式并且给它赋予一个ID号,你就可以在你的geometryplacemarkoverlay中使用这个样式。因为一个元素可以使用同一样式。这种方式定义和使用的样式被称作共享样式。你一次定义一个给定的样式,然后你就可以多次引用它(使用<styleurl>元素),如果这个样式定义在同一文件中,在styleid中加上#符号。如果这个style定义是一个外部文件,在<styleurl>元素中包含整个url地址

           KML Samples文件包含了许多共享的样式,每一个在文件的开头都赋予了一个ID。注意如果你的IDS是描述性的字符串是最简单的,这样就可以很容易的分辨效果。这个有一个style的例子(transBluePoly),其定义了给一个多边形定义了一个蓝色的透明色和给这个多边形的边界定义了1.5宽度的线。这个样式用于google campus 例子的Building 41(Polygons folder)

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/kml/2.2">

     <Document>

        <Style id="transBluePoly">

          <LineStyle>

            <width>1.5</width>

          </LineStyle>

          <PolyStyle>

            <color>7dff0000</color>

          </PolyStyle>

        </Style>
        <Placemark>

          <name>Building 41</name>

          <styleUrl>#transBluePoly</styleUrl>

          <Polygon>

            <extrude>1</extrude>

            <altitudeMode>relativeToGround</altitudeMode>

            <outerBoundaryIs>

              <LinearRing>

                <coordinates> -122.0857412771483,37.42227033155257,17

                  -122.0858169768481,37.42231408832346,17

                  -122.085852582875,37.42230337469744,17

                  -122.0858799945639,37.42225686138789,17

                  -122.0858860101409,37.4222311076138,17

                  -122.0858069157288,37.42220250173855,17

                  -122.0858379542653,37.42214027058678,17

                  -122.0856732640519,37.42208690214408,17

                  -122.0856022926407,37.42214885429042,17

                  -122.0855902778436,37.422128290487,17

                  -122.0855841672237,37.42208171967246,17

                  -122.0854852065741,37.42210455874995,17

                  -122.0855067264352,37.42214267949824,17

                  -122.0854430712915,37.42212783846172,17

                  -122.0850990714904,37.42251282407603,17

                  -122.0856769818632,37.42281815323651,17

                  -122.0860162273783,37.42244918858722,17

                  -122.0857260327004,37.42229239604253,17

                  -122.0857412771483,37.42227033155257,17
                </coordinates>

              </LinearRing>

            </outerBoundaryIs>

          </Polygon>

        </Placemark>

     </Document>

    </kml>

    2.2、对于高亮的Icon的样式

           Styles and Markup文件夹中”Highlighed Icon”显示怎样使用KML创建一个roll-over效果。这个文档定义了两个样式,一个是为了”normalPlacemark”和一个”highlightPlacemark”(当这个鼠标经过这个图标时)<StyleMap>元素有两个key/value对,用来映射每一个icon状态的style。有两个icon 状态:normalhighligth.

    下面是基本的步骤:

    为一个placemark的正常图标定义<style>并且赋予他一个id(这里是”normalPlacemark”).这个样式包含一个<icon>标签,当中<href>标签为这个实际的图片位置。

    定义一个placemarkhightlight样式并且赋予一个ID给他(这里是”highlightPlacemark”)

    创建一个<StyleMap>元素并且赋予一个ID给它(“exampleStyleMap”).这个placemark将引用这个id.

    StyleMap元素中指定”#normalPlacemark”为正常状态。

    StyleMap元素中指定”#highlightPlacemark”higthlight状态。

    在一个placemark中,增加一个<styleurl>元素指定”#exampleStyleMap”

    <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/kml/2.2">

     <Document>

        <name>Highlighted Icon</name>

        <description>Place your mouse over the icon to see it display the new icon</description>

        <Style id="highlightPlacemark">

          <IconStyle>

            <Icon>

              <href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href>

            </Icon>

          </IconStyle>

        </Style>

        <Style id="normalPlacemark">

          <IconStyle>

            <Icon>

              <href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</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>Roll over this icon</name>

          <styleUrl>#exampleStyleMap</styleUrl>

          <Point>

            <coordinates>-122.0856545755255,37.42243077405461,0</coordinates>

          </Point>

        </Placemark>

     </Document>

    </kml>

    2.3 覆盖图层

           覆盖图层不能直接在Google Earth中编辑,这样创建地面覆盖图层就更加困难了。一个综合性例子可以在Screen Overlays 文件夹中找到。

           作为一个例子,在KML Sample文件中激活”Absolute Positioning:Top left”文件夹并且你可以看到在窗口试图的左上角一个覆盖层。这可以使用下面这段KML code来创建

            <?xml version="1.0" encoding="UTF-8"?>

    <kml xmlns="http://earth.google.com/kml/2.2">

     <ScreenOverlay>

        <name>Absolute Positioning: Top left</name>

        <Icon>

          <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>

        </Icon>

        <overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>

        <screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>

        <rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>

        <size x="0" y="0" xunits="fraction" yunits="fraction"/>

     </ScreenOverlay>

    </kml>
  • 相关阅读:
    【转】深入浅出单实例SINGLETON设计模式
    【转】bat等大公司常考java多线程面试题
    java递归逆置一个字符串
    求连续数组子序咧的最大和
    小程序new Date()).getMonth()拿到的月份比实际时间少一个月
    小程序云函数查询数据库时result一直为null
    小程序云开发使用where查询遇到的问题
    小程序运行报错: navigateTo:fail page "pages/navigate/navigate" is not found?
    在Thinkphp中使用AJAX实现无刷新分页
    MYSQL优化9大法!
  • 原文地址:https://www.cnblogs.com/yukun/p/1158451.html
Copyright © 2011-2022 走看看