简体   繁体   English

从kml获取坐标

[英]get coordinates from kml

I have a number of kml files with just 1 path made in each. 我有许多kml文件,每个文件只有1条路径。 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? 如何从kml中获取坐标并将其转换为数组,并将每组坐标嵌套在自己的数组中?

example array: [[lat1, long1],[lat2, long2],[lat3,long3]] 示例数组:[[lat1,long1],[lat2,long2],[lat3,long3]]

I would prefer javascript, but I can do php as well. 我更喜欢javascript,但是我也可以做php。

example kml: http://98.214.131.200/Routes/test.kml 示例kml:http: 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). geoxml3可用作独立的解析器(尽管在该模式下未经过特别良好的测试)。 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: 如果每个kml文件都有一个“路径”,则该数组可能是:

placemarks[0].LineString[0].coordinates

And is also available as a google.maps.Polyline object. 并且还可以作为google.maps.Polyline对象使用。

Here is your example kml displayed by geoxml3 , if you poke around the page with a debugger, you can see the array of coordinates. 这是geoxml3显示的示例kml,如果使用调试器在页面上戳一下 ,则可以看到坐标数组。

Use ie the SimpleXMl which is std enabled by default in most php distributions these days: 使用这些天,即大多数PHP发行版中默认启用std的SimpleXMl:

 $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);

 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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