问题
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);
?>
Heres the KML structure:
<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>
回答1:
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:
- You're not closing the
<description>
tag properly - The opening tag and closing tag doesn't match
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;
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]);
}
Output:
Array
(
[0] => Array
(
[0] => 14.8184806108
[1] => 56.8630456924
[2] => 196.0000000000
)
)
Demo!
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);
This will generate the following array:
Array
(
[0] => 14.8184806108
[1] => 56.8630456924
[2] => 196.0000000000
)
Demo!
UPDATE
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);
回答2:
$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);
}
Demo: https://eval.in/65272
来源:https://stackoverflow.com/questions/19887194/php-get-coordinates-from-kml-file