Google地球中的KML文件显示实体替换的名称

时间:2013-08-28 18:03:24

标签: kml google-earth

我有一个问题,提到KML文档中的BalloonStyle教程: BalloonStyle Documentation

使用此KML文件:

<kml xmlns="http://www.opengis.net/kml/2.2"> 
<Document>   
  <name>ExtendedData+SchemaData</name>   
  <open>1</open>    
  <!-- Create a balloon template referring to the user-defined type -->
  <Style id="trailhead-balloon-template">     
    <BalloonStyle>       
      <text>
        <![CDATA[         
          <h2>My favorite trails!</h2>         
          <br/><br/>         
          The $[TrailHeadType/TrailHeadName/displayName] is <i>$[TrailHeadType/TrailHeadName]</i>.        
          The trail is $[TrailHeadType/TrailLength] miles.         <br/>        
          The climb is $[TrailHeadType/ElevationGain] meters.         <br/><br/>       
        ]]>
      </text>     
    </BalloonStyle>
  </Style>     

  <!-- Declare the type "TrailHeadType" with 3 fields -->
  <Schema name="TrailHeadType" id="TrailHeadTypeId">     
    <SimpleField type="string" name="TrailHeadName">       
      <displayName><![CDATA[<b>Trail Head Name</b>]]></displayName>     
    </SimpleField>     
    <SimpleField type="double" name="TrailLength">       
      <displayName><![CDATA[<i>The length in miles</i>]]></displayName>     
    </SimpleField>     
    <SimpleField type="int" name="ElevationGain">       
      <displayName><![CDATA[<i>change in altitude</i>]]></displayName>     
    </SimpleField>   
  </Schema>       

  <!-- Instantiate some Placemarks extended with TrailHeadType fields -->    
  <Placemark>     
    <name>Easy trail</name>     
    <styleUrl>#trailhead-balloon-template</styleUrl>     
    <ExtendedData>       
      <SchemaData schemaUrl="#TrailHeadTypeId">        
        <SimpleData name="TrailHeadName">Pi in the sky</SimpleData>         
        <SimpleData name="TrailLength">3.14159</SimpleData>         
        <SimpleData name="ElevationGain">10</SimpleData>       
      </SchemaData>     
    </ExtendedData>     
    <Point>       
      <coordinates>-122.000,37.002</coordinates>     
    </Point>   
  </Placemark>    
  <Placemark>     
    <name>Difficult trail</name>     
    <styleUrl>#trailhead-balloon-template</styleUrl>     
    <ExtendedData>
      <SchemaData schemaUrl="#TrailHeadTypeId">         
        <SimpleData name="TrailHeadName">Mount Everest</SimpleData>        
        <SimpleData name="TrailLength">347.45</SimpleData>         
        <SimpleData name="ElevationGain">10000</SimpleData>       
      </SchemaData>    
    </ExtendedData>    
    <Point>       
      <coordinates>-121.998,37.0078</coordinates>     
    </Point>   
  </Placemark>   
</Document> 
</kml>

当您删除其中一个SimpleData元素时,Google地球会显示气球模板中的实体替换。例如,您删除了行<SimpleData name="ElevationGain">10000</SimpleData> 它在气球中显示为“爬升是$ [TrailHeadType / ElevationGain]米。”哪个看起来不太好看。

知道如何告诉Google地球,如果缺少SimpleData,它应该不显示气球中的行吗?

先谢谢

2 个答案:

答案 0 :(得分:0)

我不认为 *你可以在这里做你所要求的 - 即在这个样式文本中应用条件逻辑。我过去有类似的要求(我的某些地标中缺少某些数据)我最终在生成KML时为每种可能的缺失信息场景应用了不同的样式。

它感觉非常笨重,如果你有大量的场景,一个或多个物品丢失,而其他物品存在,很快就会变得一团糟。

*会喜欢错。

<强>更新

只是看了我的代码,我实际上最终将所有文本放在我的架构的地标字段中,并在我没有数据时打印出空节点 - 也不那么优雅,但不需要额外的样式到底。基于您的示例,例如:

<BalloonStyle>       
  <text>
    <![CDATA[         
      <h2>My favorite trails!</h2>         
      $[TrailHeadType/TrailHeadName/displayName] 
      $[TrailHeadType/TrailLengthBalloonText]               
      $[TrailHeadType/ElevationGainBalloonText]              
    ]]>
  </text>     
</BalloonStyle>

<Placemark>     
 <name>Difficult trail</name>     
 <styleUrl>#trailhead-balloon-template</styleUrl>     
 <ExtendedData>
  <SchemaData schemaUrl="#TrailHeadTypeId">         
    <SimpleData name="TrailHeadName">Mount Everest</SimpleData>        
    <SimpleData name="TrailLength">347.45</SimpleData>         
    <SimpleData name="ElevationGain">10000</SimpleData> 

    <SimpleData name="TrailHeadNameBalloonText"><![CDATA[The trail name is Mount Everest</br>]]></SimpleData>        
    <SimpleData name="TrailLengthBalloonText"><![CDATA[The trail is 347.45 miles.</br>]]></SimpleData>         
    <SimpleData name="ElevationGainBalloonText"><![CDATA[The climb is 10000 meters.</br>]]></SimpleData> 
  </SchemaData>    
 </ExtendedData>    
 <Point>       
  <coordinates>-121.998,37.0078</coordinates>     
 </Point>   
</Placemark>   

然后,如果没有数据,则将该项留空:

<Placemark>     
 <name>Difficult trail</name>     
 <styleUrl>#trailhead-balloon-template</styleUrl>     
 <ExtendedData>
  <SchemaData schemaUrl="#TrailHeadTypeId">         
    <SimpleData name="TrailHeadName">Mount Everest</SimpleData>        
    <SimpleData name="TrailLength">347.45</SimpleData>         
    <SimpleData name="ElevationGain">10000</SimpleData> 

    <SimpleData name="TrailHeadNameBalloonText"><![CDATA[The trail name is Mount Everest</br>]]></SimpleData>        
    <SimpleData name="TrailLengthBalloonText"></SimpleData>         
    <SimpleData name="ElevationGainBalloonText"></SimpleData> 
  </SchemaData>    
 </ExtendedData>    
 <Point>       
  <coordinates>-121.998,37.0078</coordinates>     
 </Point>   
</Placemark> 

答案 1 :(得分:0)

由于我的数据经常以某种方式发生变化,我现在提供一个链接到css文件的xml文件,并将其作为iframe嵌入到描述中。要离线使用,请创建kmz并将xml和css放入其中。

对于一个小教程,请看一下: http://www.w3schools.com/xml/xml_display.asp

在kml中,它看起来如下:

<description>
    <![CDATA[
        <iframe src="http://www.w3schools.com/xml/cd_catalog_with_css.xml"></iframe>
    ]]>
</description>