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

从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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化