我已经编写了这段代码来将 GPX 转换为 KMl,但是如何设置样式和更多 dom。
function gpxtokml($path,$id){
$name_file=$path;
$point=explode(".",$name_file);
$namekml=$point[0].'.kml';
$xml = simplexml_load_file($name_file);$i=0;
$arry=array();
foreach($xml->trk->trkseg->trkpt as $trkpt) {
//$arry[$i++]=$this->xml2array ($trkpt,$out = array());
foreach ( (array) $trkpt as $index => $node ){
//$out[$index] = ( is_object ( $node ) );
if(is_object ( $node )){
foreach ( (array) $trkpt as $index => $node )
$out[$index] = $node ;
continue;
}else{
$out[$index] = $node ;
}
}
$arry[$i++]=$out;
}
//print_r($arry);exit;
$retrn=$this->generatekml($arry,true,$namekml,$id);
return $retrn;
}
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
function generatekml($input,$file,$filename,$id){
$output="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<kml xmlns=\"http://www.opengis.net/kml/2.2\">
<Document>
";
$i=1;
//echo '<pre>';print_r($input);exit;
foreach($input as $key=>$point){
$name="point ".$i++;
$description='';
$lat=$point['@attributes']['lat'];
$lon=$point['@attributes']['lon'];
$coordinates=$lat .",".$lon;
$output.="<Placemark>
<name>$name</name>
<description>$description</description>
<Point>
<coordinates>$coordinates</coordinates>
</Point>
</Placemark>
";
}
$output.="</Document>
</kml>
";
if($file){
//header("Content-type: octet/stream");
//header("Content-disposition: attachment; filename=".$filename.";");
// header("Content-lenght: ".filesize("files/".$file));
//echo $output;
$fl=time().'kml.kml';
$xmlfile=WWW_ROOT.'kmlfile/'.$fl;
//echo $this->EventDetail->id=$id;
//exit;
//$date['EventDetail']['kmlfile']=time().'kml.kml';
//$this->EventDetail->save($date['EventDetail'],false);
$fp = fopen($xmlfile, 'w');
fwrite($fp, $output);
fclose($fp);
//echo time().'kml.kml';
return $fl;
}else{
}
}