I wrote this code to convert GPX to KMl, but how to set the style and more 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) {
foreach ( (array) $trkpt as $index => $node ){
if(is_object ( $node )){
foreach ( (array) $trkpt as $index => $node )
$out[$index] = $node ;
continue;
}else{
$out[$index] = $node ;
}
}
$arry[$i++]=$out;
}
$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;
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){
$fl=time().'kml.kml';
$xmlfile=WWW_ROOT.'kmlfile/'.$fl;
$fp = fopen($xmlfile, 'w');
fwrite($fp, $output);
fclose($fp);
return $fl;
}else{
}
}
user4931222
source
share