使用 DrawOrder 為 LineString 設定複雜樣式

Josh Livni,Google Geo API 團隊
2010 年 12 月

目標

本教學課程將介紹幾個「Google 地球」6.0 版導入的新 KML 元素,以及如何利用這些元素建立您線條樣式的複雜製圖。您要瞭解的元素包括 <gx:physicalWidth><gx:outerColor><gx:outerWidth><gx:drawOrder>

簡介

在推出「Google 地球」6.0 版之前,您無法限制 LineString 的複雜製圖樣式。舉例來說,如果您想為道路建立樣式,其中 LineString 的中心顏色和邊緣不同,您就必須繪製兩個不同寬度的 LineString,就算是不可保證的繪製順序也一樣。

以下範例會指導您建立道路。第一,路面就是簡單的風格。然後新增人行道。最後,您必須新增高速公路的高架橋。

基本道路

第一個感興趣的元素是 <gx:physicalWidth>,可讓您將 LineString 的寬度設定為公尺,而非像素。以道路範例為例,這表示您可以將道路的寬度設定為與基本圖像相符,無論使用者從哪條高度觀看道路也是如此。隨著您高度的增加,道路就會減少像素值,只有在您放大時才會看到 LineString 。如果您不想強調遠處的小道路,或想要確保製圖永遠與衛星影像相符,這個方法就非常實用。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
  <name>Road Styling</name>
  <Style id="street">
    <LineStyle>
      <color>ff235523</color>
      <gx:physicalWidth>12</gx:physicalWidth>
    </LineStyle>
  </Style>
  <Placemark>
    <styleUrl>#street</styleUrl>
    <LineString>
      <coordinates>
      -122.2442883478408,37.4347536724074,0
      -122.2417741446485,37.43594997501623,0
      -122.2414951359056,37.43611878445952,0
      </coordinates>
    </LineString>
  </Placemark>
</Document>
</kml>

變更 Google 地球的相機高度時,請注意圖像的高度與基礎圖像的寬度是否一致。

新增人行道

只要在道路的邊緣上增加淺綠色,即可新增 <gx:outerColor><gx:outerWidth> 元素,用來指出人行道。將道路總像素的 25% 設為淺灰色。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
  <name>Road Styling</name>
  <Style id="street_sidewalk">
    <LineStyle>
      <color>ff235523</color>
      <gx:physicalWidth>12</gx:physicalWidth>
      <gx:outerColor>ff55ff55</gx:outerColor>
      <gx:outerWidth>0.25</gx:outerWidth>
    </LineStyle>
  </Style>
  <Placemark>
    <styleUrl>#street_sidewalk</styleUrl>
    <LineString>
      <coordinates>
      -122.2442883478408,37.4347536724074,0
      -122.2417741446485,37.43594997501623,0
      -122.2414951359056,37.43611878445952,0
      </coordinates>
    </LineString>
  </Placemark>
</Document>
</kml>

由於道路的寬度設定為 12 公尺,外部寬度的 .25 值表示該寬度的 3 公尺將設為較淺的綠色 (道路每側約 1.5 公尺)。

高速公路天橋

現在您已經有基本道路,接下來該踏上高速公路高架橋了。這個顏色是淺橘色和深橘色的中線。這裡的重點在於在 <LineSring> 中加入 <gx:drawOrder> 的 1 (預設值為 0),確保公路會在道路上方顯示。如果您正在建構更複雜的公路交會 (或任何需要超過 2 個重疊的 LineString) 的繪圖,您可以在想要在頂端顯示的變化版本中,為 <gx:drawOrder> 新增較高的值。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
  <name>Road Styling</name>
  <Style id="street_sidewalk">
    <LineStyle>
      <color>ff235523</color>
      <gx:physicalWidth>10</gx:physicalWidth>
      <gx:outerColor>ff55ff55</gx:outerColor>
      <gx:outerWidth>0.25</gx:outerWidth>
    </LineStyle>
  </Style>
  <Style id="highway">
    <LineStyle>
      <color>cc1447ff</color>
      <gx:physicalWidth>20</gx:physicalWidth>
      <gx:outerColor>cc1473ff</gx:outerColor>
      <gx:outerWidth>0.75</gx:outerWidth>
    </LineStyle>
  </Style>
  <Placemark>
    <styleUrl>#street_sidewalk</styleUrl>
    <LineString>
      <coordinates>
     -122.2442883478408,37.4347536724074,0
     -122.2417741446485,37.43594997501623,0
     -122.2414951359056,37.43611878445952,0
      </coordinates>
    </LineString>
  </Placemark>
  <Placemark>
    <styleUrl>#highway</styleUrl>
    <LineString>
    <gx:drawOrder>1</gx:drawOrder>
    <coordinates>
      -122.2442692500139,37.43634904345254,0
      -122.2415928723012,37.43416417520744,0
    </coordinates>
    </LineString>
  </Placemark>
</Document>
</kml>

後續步驟

以單一顏色描述單一 LineString 並結合顯示順序,您就能建立先前難以使用的複雜線條圖。無論是要建立高速公路高架橋,還是抽象藝術專案,都能透過這些樣式規則享受樂趣!