从PHP

时间:2016-03-19 05:39:41

标签: php xml laravel character-encoding kml

在尝试使用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>

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

其他<styleUrl>标签也很少,但效果很好。我尝试删除所有PHP的东西,只放置静态KML但仍输出Url>。知道什么可能是错的吗?

修改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:

我尝试将<styleUrl>标记向上移动一点(虽然这在语法上是不正确的,但只是为了得到这个想法)并且只要它不是<kml>标记的子代,它就能正常工作办法。即在<?xml?>标记之后放置它可以正常工作。

1 个答案:

答案 0 :(得分:0)

虽然不是理想的解决方案,但我在echo '<style';标记之前添加了额外的<styleUrl>,从而找到了解决方法。我很想知道为什么会这样,我真的怀疑任何人都会遇到同样的问题。 :d