简体   繁体   中英

get coordinates from kml

I have a number of kml files with just 1 path made in each. How can I get the coordinates from the kml and turn them into an array with each set of coordinates nested inside within their own array?

example array: [[lat1, long1],[lat2, long2],[lat3,long3]]

I would prefer javascript, but I can do php as well.

example kml: http://98.214.131.200/Routes/test.kml

geoxml3 can be used as a stand-alone parser (although it is not particularly well tested in that mode). If you want polylines, you probably want the polys branch rather than the trunk.

The array of coordinates is not quite in the format you requested, it looks like this:

placemarks[].LineString[].coordinates[].{lat:Float, lng:Float, alt:Float}

If each of your kml files has one "path", probably the array would be:

placemarks[0].LineString[0].coordinates

And is also available as a google.maps.Polyline object.

Here is your example kml displayed by geoxml3 , if you poke around the page with a debugger, you can see the array of coordinates.

Use ie the SimpleXMl which is std enabled by default in most php distributions these days:

 $url = "http://98.214.131.200/Routes/test.kml";
 $contents = file_get_contents($url);
 $xml      = new SimpleXMLElement($contents);
 $value    = (string)$xml->Document->Placemark->LineString->coordinates;
 $values   = explode(" ", trim($value));
 $coords   = array();
 foreach($values as $value) {    
    $args     = explode(",", $value);
    $coords[] = array($args[0], $args[1]);
 }
 echo '<pre>';
 print_r($coords);
<?php
$xmlFile = '73---5.kml';
$xmlString = file_get_contents($xmlFile);
$doc = new DomDocument();
$doc->loadXML($xmlString);
$xpath = new DomXpath($doc);
$ward_names=array();
$searchNodes = $doc->getElementsByTagName( "coordinates" );   
foreach( $searchNodes as $searchNode ) 
{ 

   $ward_names [] =  $searchNode->nodeValue; 

}


 $string_version = implode(' ', $ward_names) ;
 $destination_array = explode(' ', $string_version);


echo "<pre>";
 print_r($destination_array);

 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM