drxm72811 2016-03-19 05:39
浏览 127
已采纳

从PHP导出KML文件

I'm facing a weird problem while trying to dynamically create an KML file (which is basically just XML) with PHP. I'm using the laravel framework and here's the view that generates the KML file.

<?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>

I exported a single layer from google maps and changed it as how I wanted. And this works just fine. But the only problem is that the first occurrence <styleUrl> tag in the code, when the file is downloaded, is just Url>.

There are also few other <styleUrl> tags but they work just fine. I tried remove all the PHP stuff and putting just static KML but still it outputs Url>. Any idea what could be wrong?

Edit 1 :

Variables being passed to the template are $paths and $points. Here they are in case it's any help.

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

Edit 2 :

I tried to move the <styleUrl> tag up a little bit (although that is syntactically incorrect but just to get the idea) and it works fine as long as it's not child of <kml> tag in any way. i.e. putting it after <?xml?> tag works fine.

  • 写回答

1条回答 默认 最新

  • dongmanni6916 2016-03-21 07:16
    关注

    While not an ideal solution, I found a workaround by putting an extra echo '<style'; before the <styleUrl> tag. I'd love to know why that happened though and I really doubt that anyone will face the same problem. :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题