Google My Maps中的KML格式

這篇要來紀錄一下,如何產生一個 Google My Maps 能夠閱讀、解析、顯示的 KML 內容格式。

 

KML範例檔案的取得

直接從Google My Maps建立地圖並匯出KML

最簡單的實驗就是先到 Google My Maps 上面建立一個地圖,然後點擊地圖名稱右側的「Google設定按鈕」,然後選擇「匯出成 KML/KMZ」。這樣就能觀察由 Google My Maps 產生的KML檔案格式,方便自己生成KML檔時進行驗證或比對。

Google My Maps匯出KML

參考Google KML說明中的範例檔案

🔗 Google 的 KML範例檔案

 

KML的格式內容

這邊我們就以 Google My Maps 可以讀懂的 KML 單元來做實驗與說明。為什麼說「Google My Maps可以讀懂」,是因為坊間一些App所匯出的KML檔案,在你從App匯出並匯入Google My Maps後,會發現一些格式或單元不相容,而導致匯入失敗的情況。

新增一個地標

最簡單 Google My Maps 可以匯入的 KML 檔案,大概就類似下方這樣包含一個地標(Placemark)的內容。Placemark中以<name>來描述這個地標的名稱,以<description>紀錄地點的說明,以及透過<Point>來描述這個點的座標位置。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> 
	<Placemark>
		<name>地標1</name>
		<description></description>
		<Point>
			<coordinates>121.41738,25.11393,0</coordinates>
		</Point>
	</Placemark> 
</kml>

在<Placemark>中使用<Point>來紀錄一個點,透過<coordinates>來描述座標的位置。裡面的內容為:

<coordinates>經度,緯度,高度</coordinates>

高度指的是海拔高度,單位為公尺。

地圖的座標支援種類有哪些?依照Google Maps Help上的說明,目前有支援以下幾種:

  • Decimal degrees (DD): 41.40338, 2.17403
  • Degrees, minutes, and seconds (DMS): 41°24’12.2″N 2°10’26.5″E
  • Degrees and decimal minutes (DMM): 41 24.2028, 2 10.4418

 

那如何知道一個地點的座標數值呢?最簡單的方式就是開啟Google Maps,在你所要查詢座標的位置點點擊滑鼠右鍵,這樣就會跳出一個功能方塊,最上面的數值就是這個位置的座標。

Google Maps顯示座標

在想要進一步新增地標時,會發現到兩個情況。

  1. 匯入 Google My Maps 時,必須要在有空圖層的情況下,才能匯入KML資料。
  2. 無法新增超過一個Placemark的話,在上面範例中,如果依樣畫葫蘆,只會顯示最後一筆的地標資料。

加入圖層

上面遇到這兩個問題可以合起來說明,就是在問題1的情況下可以知道,基本上在Google My Maps的設計上,地標基本上都要包含於一個圖層之內的。而圖層在這裡對應的XML標籤名稱為「Folder」,在圖層「Folder」的兩個基本屬性為名稱與說明,名稱的XML標籤為「name」、說明的XML標籤為「description」,這兩個屬性對應到Google My Maps中,就是點擊圖層時,上面出現的一列名稱,以及下方可以容納多列資料的說明空間。然後再將多個地標(Placemarks)放到圖層(Folder),這樣就能在一個圖層中正確顯示多個地標,同時解決了上面段落的兩個問題。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> 
	<Folder>
		<name>Placemarks</name>
		<description></description>
	
		<Placemark>
		    <name>地標1</name>
		    <description></description>
		    <Point>
		  	    <coordinates>121.41738,25.11393,0</coordinates>
		    </Point>
	    </Placemark>

        <Placemark>
		    <name>地標2</name>
		    <description></description>
		    <Point>
		  	    <coordinates>121.41838,25.11293,0</coordinates>
		    </Point>
	    </Placemark> 
	</Folder>
</kml>

路徑繪製

路徑的部份同樣是用<Placemark>,只不過之前單一地點是以<point>來標明,路徑的話是以<LineString>來描述。<LineString>主要是透過<coordinates>標籤來描述一連串的路徑資料,關於<coordinates>內的資料格式,可以參考上面「新增一個地標」章節中的說明,另外,<tessellate>屬性就字面上的翻譯是「鑲嵌」,屬性是個boolean值,當為1的時候為True,代表這條LineString要鑲嵌在地形上,也就是路徑隨著這地形高低而起伏,因此在絕大部分一般的使用上,應該都是設為1。

<Folder>
      <name>二子坪步道步行路線</name>
      <Placemark>
        <name>二子坪步道步行路線</name>
        <LineString>
          <tessellate>1</tessellate>
          <coordinates>
            121.52529,25.18593,0
            121.5252,25.18584,0
            121.52512,25.18577,0
            121.52483,25.18556,0
            121.52475,25.18552,0
            121.52452,25.18538,0
            121.5244,25.18531,0
            121.5242,25.18517,0
            121.52405,25.18508,0
            121.52403,25.18507,0
            121.52398,25.18505,0
            121.52392,25.18503,0
            121.52385,25.18502,0
            121.52384,25.18502,0
            121.52371,25.18499,0
            121.52366,25.18496,0
            121.52358,25.18493,0
            121.52348,25.18488,0
            121.52347,25.18487,0
            121.52334,25.18479,0
            121.52332,25.18478,0
            121.52307,25.18462,0
            121.52282,25.18448,0
            121.52274,25.18444,0
            121.52254,25.18438,0
            121.52226,25.1843,0
            121.52218,25.18428,0
            121.52188,25.18419,0
            121.52163,25.18412,0
            121.5215,25.18409,0
            121.52141,25.18407,0
            121.52131,25.18404,0
            121.52129,25.18403,0
            121.52123,25.18398,0
            121.52111,25.18386,0
            121.52107,25.18383,0
            121.52102,25.1838,0
            121.52094,25.18379,0
            121.52086,25.1838,0
            121.52082,25.18381,0
            121.5208,25.18382,0
            121.52071,25.18384,0
            121.52063,25.18386,0
            121.52045,25.1839,0
            121.52044,25.1839,0
            121.52026,25.18394,0
            121.52024,25.18394,0
            121.52013,25.18393,0
            121.52004,25.18393,0
            121.51997,25.18393,0
            121.51986,25.1839,0
            121.51985,25.1839,0
            121.51955,25.18382,0
            121.51927,25.18377,0
            121.51924,25.18376,0
            121.51907,25.18373,0
            121.51888,25.1837,0
            121.5188,25.18369,0
            121.51868,25.18368,0
            121.51848,25.18366,0
            121.51831,25.18364,0
            121.51829,25.18364,0
            121.51811,25.1836,0
            121.51809,25.18359,0
            121.51787,25.18355,0
            121.51771,25.18351,0
            121.51762,25.18349,0
            121.51753,25.18345,0
            121.51748,25.1834,0
            121.51743,25.18335,0
            121.5174,25.1833,0
            121.51738,25.18328,0
            121.51732,25.18322,0
            121.51729,25.18321,0
            121.51725,25.18319,0
            121.51724,25.18318,0
            121.51718,25.18317,0
            121.51712,25.18316,0
            121.51706,25.18315,0
            121.51695,25.18314,0
            121.51687,25.18312,0
            121.5168,25.1831,0
            121.51674,25.18307,0
            121.51669,25.18304,0
            121.51666,25.18299,0
            121.51661,25.18289,0
            121.51661,25.18287,0
            121.51653,25.1826,0
            121.51651,25.18253,0
            121.5165,25.18246,0
            121.5165,25.18228,0
            121.51652,25.18218,0
            121.51664,25.18202,0
            121.51666,25.18199,0
            121.5167,25.18191,0
            121.51671,25.18186,0
            121.51671,25.18182,0
            121.51671,25.18179,0
            121.51669,25.18175,0
            121.51667,25.18171,0
            121.51651,25.18155,0
            121.51648,25.1815,0
            121.51646,25.18146,0
            121.51645,25.18142,0
            121.51644,25.18139,0
            121.51644,25.18138,0
            121.51641,25.18124,0
            121.51642,25.18121,0
            121.51642,25.18111,0
            121.51643,25.18104,0
            121.51644,25.18101,0
            121.51645,25.1809,0
            121.51644,25.18086,0
            121.51644,25.18085,0
            121.51644,25.18082,0
            121.51644,25.18077,0
            121.51646,25.18073,0
            121.51647,25.18068,0
            121.5165,25.18064,0
            121.51654,25.18054,0
            121.51656,25.18052,0
            121.51657,25.18048,0
            121.51658,25.18045,0
            121.51659,25.18042,0
            121.5166,25.1804,0
            121.5166,25.18034,0
            121.5166,25.18032,0
            121.51659,25.18028,0
            121.51656,25.18022,0
            121.51653,25.18018,0
            121.51644,25.18007,0
            121.51642,25.18003,0
            121.51625,25.17971,0
            121.51623,25.17968,0
            121.51605,25.17942,0
            121.51604,25.1794,0
            121.516,25.17924,0
            121.51599,25.17908,0
            121.51603,25.17882,0
            121.51592,25.17877,0
            121.51589,25.17876,0
            121.51581,25.17875,0
            121.51576,25.17874,0
            121.51569,25.17873,0
            121.51563,25.17872,0
            121.51555,25.1787,0
            121.5155,25.17869,0
            121.51546,25.17868,0
            121.51542,25.17866,0
            121.51541,25.17866,0
            121.51536,25.17863,0
            121.51533,25.17859,0
            121.51526,25.17853,0
            121.51521,25.17848,0
            121.51519,25.17846,0
            121.51518,25.17844,0
            121.51516,25.17841,0
            121.51514,25.17837,0
            121.51513,25.17835,0
            121.51512,25.17831,0
            121.5151,25.17828,0
            121.51507,25.17823,0
            121.51489,25.17813,0
            121.51462,25.17803,0
            121.51453,25.17799,0
            121.51447,25.17797,0
            121.51445,25.17796,0
            121.51443,25.17795,0
            121.5144,25.17794,0
            121.51437,25.17792,0
            121.51433,25.17791,0
            121.51431,25.1779,0
          </coordinates>
        </LineString>
      </Placemark>

參考資料: