如何解析PHP中的数组中的KML文件(用于谷歌地图)?

时间:2012-11-07 11:05:35

标签: xml maps kml

我想解析KML文件并放入数组,以便我可以更好地访问其元素。 我是否需要在php中以XML方式解析它?

2 个答案:

答案 0 :(得分:1)

就像:

function parseKML1(){
    $contents = file_get_contents('cotest.kml'); // filename
 $xml      = new SimpleXMLElement($contents);  //getting the contents
 $value    = (string)$xml->Document->Folder->Placemark->Polygon->outerBoundaryIs->LinearRing->coordinates; ///KML Structure
 $values   = explode(" ", trim($value));
 $coords   = array();
 foreach($values as $value) {    
    $args     = explode(",", $value);
    $coords[] = array($args[0], $args[1],$args[2]);
 }

 echo('<pre>');
 print_r($coords);  //printing the array

 }

就是这样,我们将数据解析成php中的数组

<kml><Document><Folder><name>sql_statement</name><Schema name="sql_statement" id="sql_statement"></Schema><Placemark><name>Hall                                                                                      </name><ExtendedData></ExtendedData><Polygon><outerBoundaryIs><LinearRing><coordinates>-100.944938999999906,34.748280999999963,0 -100.84772799999989,34.748310999999966,0 -100.655293999999884,34.748058999999962,0 -100.600692999999907,34.747958999999966,0 -100.572734999999895,34.747869999999963,0 -100.555416999999892,34.747772999999967,0 -100.54070299999988,34.747722999999965,0 -100.523008999999888,34.747637999999966,0 -100.415894999999907,34.747524999999968,0 -100.415911999999906,34.590452999999961,0 -100.415797999999882,34.561719999999966,0 -100.415795999999887,34.561262999999961,0 -100.415880999999899,34.528678999999961,0 -100.415949999999896,34.517005999999967,0 -100.415949999999896,34.515623999999967,0 -100.41773099999989,34.362238999999967,0 -100.417782999999901,34.313523999999965,0 -100.498192999999901,34.313526999999965,0 -100.503372999999897,34.314078999999964,0 -100.517339999999891,34.314101999999963,0 -100.624037999999899,34.314191999999963,0 -100.625403999999904,34.313398999999961,0 -100.627353999999897,34.313391999999965,0 -100.672836999999888,34.313473999999964,0 -100.682781999999904,34.313389999999963,0 -100.693753999999885,34.313298999999965,0 -100.721133999999893,34.313324999999963,0 -100.723279999999903,34.313318999999964,0 -100.728890999999891,34.313304999999964,0 -100.766540999999904,34.313280999999968,0 -100.769850999999903,34.313256999999965,0 -100.897068999999888,34.312961999999963,0 -100.897904999999895,34.313171999999966,0 -100.897904999999895,34.313034999999964,0 -100.919962999999882,34.312879999999964,0 -100.946161999999887,34.312762999999961,0 -100.94604099999988,34.314444999999964,0 -100.946118999999882,34.333875999999961,0 -100.946152999999896,34.338639999999963,0 -100.946345999999892,34.370078999999961,0 -100.945927999999896,34.379509999999968,0 -100.945818999999901,34.386378999999963,0 -100.945465999999897,34.483357999999967,0 -100.945461999999893,34.484496999999962,0 -100.945575999999903,34.491797999999967,0 -100.9455379999999,34.492196999999962,0 -100.945423999999903,34.494767999999965,0 -100.945156999999895,34.622261999999964,0 -100.945175999999904,34.627944999999961,0 -100.945176999999887,34.630168999999967,0 -100.944938999999906,34.748280999999963,0</coordinates>

答案 1 :(得分:0)

对于像这样的KML文件:

<?xml version="1.0" encoding="UTF-8" ?>
<kml>
<Document>
    <Placemark>
        <name><![CDATA[wail]]></name>
        <Polygon>
            <extrude>1</extrude>
            <altitudeMode>relativeToGround</altitudeMode>
            <outerBoundaryIs>
                <LinearRing>
                    <coordinates>
                        1.2799072000000002,41.78769700000001
                        1.005249,40.8138092
                        3.7463379000000003,41.00477540000001
                        3.652954100000001,42.1185987
                        2.208252,42.45994040000001
                        1.2799072000000002,41.78769700000001
                    </coordinates>
                </LinearRing>
            </outerBoundaryIs>
        </Polygon>
    </Placemark>
</Document>
</kml>

我使用这个功能:

function kmlToArray($filePath)
{
    if (file_exists($filePath)) {
        $xml = simplexml_load_file($filePath);
        $value    = (string)$xml->Document->Placemark->Polygon->outerBoundaryIs->LinearRing->coordinates;
        $values   = explode("\n", trim($value));
        $coords   = array();
        foreach($values as $value) {   
            $value = trim(preg_replace('/\t+/', '', $value));
            $args     = explode(",", $value);
            array_push($coords,$args[0]." ".$args[1]);
        }
    } else {
        return 'Error : Failed to open the file';
    }
    return $coords;
}