【问题标题】:Exporting a KML file from PHP从 PHP 导出 KML 文件
【发布时间】:2016-03-21 15:16:59
【问题描述】:

在尝试使用 PHP 动态创建 KML 文件(基本上只是 XML)时,我遇到了一个奇怪的问题。我正在使用 laravel 框架,这是生成 KML 文件的视图。

<?php
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="location.kml"');
?>
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
  <Document>
    <name>Towers</name>

    <?php for ($i = 0; $i < count($points); $i++){ ?>
    <Placemark>
      <name>{{ $points[$i]['name'] }}</name>
      <!--HERE'S THE PROBLEM-->
      <styleUrl>#icon-503-DB4436-nodesc</styleUrl>
      <Point>
        <coordinates>{{ $points[$i]['coords'] }}</coordinates>
      </Point>
    </Placemark>
    <?php } ?>

    <?php for ($i = 0; $i < count($paths); $i++){ ?>
    <Placemark>
      <name>{{ $paths[$i]['name'] }}</name>
      <styleUrl>#line-000000-1-nodesc</styleUrl>
      <LineString>
        <tessellate>1</tessellate>
        <coordinates>{{ $paths[$i]['coords'] }}</coordinates>
      </LineString>
    </Placemark>
    <?php } ?>

    <Style id='icon-503-DB4436-nodesc-normal'>
      <IconStyle>
        <color>ff3644DB</color>
        <scale>1.1</scale>
        <Icon>
          <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
        </Icon>
        <hotSpot x='16' y='31' xunits='pixels' yunits='insetPixels'>
        </hotSpot>
      </IconStyle>
      <LabelStyle>
        <scale>0.0</scale>
      </LabelStyle>
      <BalloonStyle>
        <text><![CDATA[<h3>$[name]</h3>]]></text>
      </BalloonStyle>
    </Style>
    <Style id='icon-503-DB4436-nodesc-highlight'>
      <IconStyle>
        <color>ff3644DB</color>
        <scale>1.1</scale>
        <Icon>
          <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
        </Icon>
        <hotSpot x='16' y='31' xunits='pixels' yunits='insetPixels'>
        </hotSpot>
      </IconStyle>
      <LabelStyle>
        <scale>1.1</scale>
      </LabelStyle>
      <BalloonStyle>
        <text><![CDATA[<h3>$[name]</h3>]]></text>
      </BalloonStyle>
    </Style>
    <StyleMap id='icon-503-DB4436-nodesc'>
      <Pair>
        <key>normal</key>
        <styleUrl>#icon-503-DB4436-nodesc-normal</styleUrl>
      </Pair>
      <Pair>
        <key>highlight</key>
        <styleUrl>#icon-503-DB4436-nodesc-highlight</styleUrl>
      </Pair>
    </StyleMap>
    <Style id='line-000000-1-nodesc-normal'>
      <LineStyle>
        <color>ff000000</color>
        <width>1</width>
      </LineStyle>
      <BalloonStyle>
        <text><![CDATA[<h3>$[name]</h3>]]></text>
      </BalloonStyle>
    </Style>
    <Style id='line-000000-1-nodesc-highlight'>
      <LineStyle>
        <color>ff000000</color>
        <width>2.0</width>
      </LineStyle>
      <BalloonStyle>
        <text><![CDATA[<h3>$[name]</h3>]]></text>
      </BalloonStyle>
    </Style>
    <StyleMap id='line-000000-1-nodesc'>
      <Pair>
        <key>normal</key>
        <styleUrl>#line-000000-1-nodesc-normal</styleUrl>
      </Pair>
      <Pair>
        <key>highlight</key>
        <styleUrl>#line-000000-1-nodesc-highlight</styleUrl>
      </Pair>
    </StyleMap>
  </Document>
</kml>

我从谷歌地图导出了一个图层,并根据我的需要进行了更改。这很好用。但唯一的问题是代码中第一个出现的&lt;styleUrl&gt;标签,当文件被下载时,只是Url&gt;

还有一些其他的&lt;styleUrl&gt; 标签,但它们工作得很好。我尝试删除所有 PHP 内容并仅放置静态 KML,但它仍然输出 Url&gt;。知道有什么问题吗?

编辑 1:

传递给模板的变量是$paths$points。他们在这里,以防万一。

// $points
array(1) {
  [0]=>
  array(2) {
    ["name"]=>
    string(9) "Nijraj Gelani"
    ["coords"]=>
    string(29) "-79.58523260,43.73280100,0.0 "
  }
}
// $paths
array(0) {
}

编辑 2:

我尝试将&lt;styleUrl&gt; 标记向上移动一点(尽管这在语法上不正确,但只是为了理解),只要它不是&lt;kml&gt; 标记的子级,它就可以正常工作。即把它放在&lt;?xml?&gt; 标签之后可以正常工作。

【问题讨论】:

  • 我能想到的唯一一件事是您在刀片解释器中触发了一个导致此问题的错误。我看到您在模板中只使用了一个刀片构造。尝试将其更改为 php 并将您的模板保存为 php 文件而不是 Blade.php 文件,看看是否有帮助。
  • 谢谢@delatbabel 但它没有用。它仍然在文件中显示Url&gt;
  • 好的,你发现了一个奇怪而奇妙的新错误。我想看看我是否可以在某个时候重现它。您下面的“解决方案”实际上只是引入了一个新错误,该错误掩盖了现有错误,但如果这是您必须做的,那么我猜您会这样做。

标签: php xml laravel character-encoding kml


【解决方案1】:

虽然不是一个理想的解决方案,但我通过在&lt;styleUrl&gt; 标记之前添加一个额外的echo '&lt;style'; 找到了一种解决方法。我很想知道为什么会这样,但我真的怀疑有人会面临同样的问题。 :D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2012-01-10
    • 2013-08-04
    相关资源
    最近更新 更多