PHP从KML文件获取坐标 [英] PHP get coordinates from KML file
问题描述
我正在使用KML文件提取坐标并将其插入到多维数组中.起初,我尝试仅在页面上输出坐标-而且似乎无法正常工作
I'm using a KML file to extract coordinates and insert them in a multi-dimensional array. At first I'm trying to output only the coordinates on the page - and it doesn't seem to be working
这是我到目前为止尝试过的代码:
Here is the code I've tried so far:
<?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>
推荐答案
您要解析的XML无效,因此您必须先对其进行修复,然后才能对其进行解析.这些是我看到的问题:
The XML you're trying to parse isn't valid and you'll need to fix that before you can parse it. These are the problems I see:
- 您没有正确关闭
<description>
标记 - 开始标签和结束标签不匹配
纠正错误后,您的XML结构应如下所示:
After fixing the mistakes, your XML structure should look like below:
<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>
您的代码如下:
Coming to your code, you have the following:
$value = (string)$xml->Document->Placemark->Point->coordinates;
您正在将$value
强制转换为字符串,因此foreach
循环将不起作用. foreach
需要一个数组作为参数,但是您将传递一个字符串.
You're casting $value
as a string, so the foreach
loop wouldn't work. foreach
requires an array as it's argument, but you'll be passing a string instead.
更正后的代码应类似于:
The corrected code should look like:
$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
数组中.如果只想获得一个坐标,则可以将其转换为字符串,然后执行以下操作:
The above code will try and get all the coordinates into the $coords
array. If you want to get only one coordinate, you can cast it as a string, then do the following:
$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文件的结构有些不同.下面的代码应该可以工作:
The structure of the KML file you posted is a bit different. The below code should work:
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);
这篇关于PHP从KML文件获取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!