2

我正在使用 KML 文件来提取坐标并将它们插入到多维数组中。起初我试图只输出页面上的坐标 - 它似乎没有工作

这是我到目前为止尝试过的代码:

<?php
$url = "myKML.kml";
$contents = file_get_contents($url);
$xml      = new SimpleXMLElement($contents);

$value    = (string)$xml->Document->Placemark->Point->coordinates;
$coords   = array();
foreach($value as $coord) {    
$args     = explode(",", $value);
$coords[] = array($args[0], $args[1], $args[2]);
}

print_r($coord);
?>

下面是 KML 结构:

<Document>
<name>...</name>
<open>1</open>
<Snippet maxLines='0'></Snippet>
<description>.../description>
<Style >
<BalloonStyle>
<text>
...
</text>
</BalloonStyle>
</Style>
<Placemark >
<name>...</name>
<styleUrl>...</styleUrl>
<Snippet maxLines='0'></Snippet>
<ExtendedData>
<Data name='__title'>
<value>...</value>
</Data>
<Data name='__imgUrl'>
<value>...</value>
</Data>
<Data name='__data'>
<value>...</value>
</Data>
</ExtendedData>
 <Point>
 <coordinates>14.8184806108,56.8630456924,196.0000000000</coordinates>
 </Point>
 </Placemark>
4

2 回答 2

3

您尝试解析的 XML 无效,您需要先修复它,然后才能解析它。这些是我看到的问题:

  • 您没有<description>正确关闭标签
  • 开始标签和结束标签不匹配

修复错误后,您的 XML 结构应如下所示:

<Document>
<name>...</name>
<open>1</open>
<Snippet maxLines='0'></Snippet>
<description>...</description>
<Style >
    <BalloonStyle>
        <text>
            ...
        </text>
    </BalloonStyle>
</Style>
<Placemark >
    <name>...</name>
    <styleUrl>...</styleUrl>
    <Snippet maxLines='0'></Snippet>
    <ExtendedData>
        <Data name='__title'>
            <value>...</value>
        </Data>
        <Data name='__imgUrl'>
            <value>...</value>
        </Data>
        <Data name='__data'>
            <value>...</value>
        </Data>
    </ExtendedData>
    <Point>
        <coordinates>14.8184806108,56.8630456924,196.0000000000</coordinates>
    </Point>
</Placemark>
</Document>

来到您的代码,您有以下内容:

$value    = (string)$xml->Document->Placemark->Point->coordinates;

您正在转换$value为字符串,因此foreach循环不起作用。foreach需要一个数组作为参数,但您将传递一个字符串。

更正后的代码应如下所示:

$value = (array) $xml->Placemark->Point->coordinates;

$coords   = array();
foreach($value as $coord) {    
    $args     = explode(",", $coord);
    $coords[] = array($args[0], $args[1], $args[2]);
}

输出:

Array
(
    [0] => Array
        (
            [0] => 14.8184806108
            [1] => 56.8630456924
            [2] => 196.0000000000
        )

)

演示!

上面的代码将尝试将所有坐标放入$coords数组中。如果只想获取一个坐标,可以将其转换为字符串,然后执行以下操作:

$value = (string) $xml->Placemark->Point->coordinates;
list($coord1, $coord2, $coord3) = explode(',', $value);
$coords = array($coord1, $coord2, $coord3);
print_r($coords);

这将生成以下数组:

Array
(
    [0] => 14.8184806108
    [1] => 56.8630456924
    [2] => 196.0000000000
)

演示!

更新

您发布的 KML 文件的结构有些不同。下面的代码应该可以工作:

foreach ($xml->Document->Placemark as $coord) {
    $coord = (string) $coord->Point->coordinates."<br/>";
    $args     = explode(",", $coord);
    $coords[] = array($args[0], $args[1], $args[2]);
}
print_r($coords);
于 2013-11-10T07:32:13.047 回答
1
$kml = new SimpleXMLElement(file_get_contents('myKML.kml'));

foreach ($kml->xpath('//Placemark/Point/coordinates') as $kml_coordinates) {
    // for legibility...
    sscanf((string) $kml_coordinates, '%f,%f,%f', $latitude, $longitude, $altitude);
    $coords[] = array($latitude, $longitude, $altitude);
    // for simplicity...
    $coords[] = explode(',', (string) $kml_coordinates);
}

演示:https ://eval.in/65272

于 2013-11-10T07:37:28.080 回答