将KML从谷歌地图解析为数组

时间:2015-09-16 16:56:26

标签: php arrays kml

我需要从单个数组中的Google地图kml文件中收集所有坐标。

kml内容为:

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
    <Document>
        <name>lago maggiore</name>
        <description><![CDATA[]]></description>
        <Folder>
            <name>Livello senza titolo</name>
            <Placemark>
                <name>Linea 1</name>
                <description><![CDATA[prima parte]]></description>
                <styleUrl>#line-000000-1</styleUrl>
                <ExtendedData>
                </ExtendedData>
                <LineString>
                    <tessellate>1</tessellate>
                    <coordinates>8.6048698,45.7246374,0.0 8.603754,45.7248771,0.0</coordinates>
                </LineString>
            </Placemark>
            <Placemark>
                <name>Linea 2</name>
                <description><![CDATA[seconda parte]]></description>
                <styleUrl>#line-000000-1</styleUrl>
                <ExtendedData>
                </ExtendedData>
                <LineString>
                    <tessellate>1</tessellate>
                    <coordinates>8.8187063,46.1729734,0.0 8.8187706,46.172913900000005,0.0</coordinates>
                </LineString>
            </Placemark>
            <Placemark>
                <name>Linea 3</name>
                <description><![CDATA[terza parte]]></description>
                <styleUrl>#line-000000-1</styleUrl>
                <ExtendedData>
                </ExtendedData>
                <LineString>
                    <tessellate>1</tessellate>
                    <coordinates>8.617798100000002,45.9111526,0.0 8.6177766,45.9110257,0.0</coordinates>
                </LineString>
            </Placemark>
        </Folder>
        <Folder>
            <name>Livello senza titolo</name>
        </Folder>
        <Folder>
            <name>Livello senza titolo</name>
        </Folder>
        <Style id='line-000000-1-normal'>
            <LineStyle>
                <color>ff000000</color>
                <width>1</width>
            </LineStyle>
        </Style>
        <Style id='line-000000-1-highlight'>
            <LineStyle>
                <color>ff000000</color>
                <width>2.0</width>
            </LineStyle>
        </Style>
        <StyleMap id='line-000000-1'>
            <Pair>
                <key>normal</key>
                <styleUrl>#line-000000-1-normal</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#line-000000-1-highlight</styleUrl>
            </Pair>
        </StyleMap>
    </Document>
</kml>

我尝试了这个,但我总是得到1个coords节点而不是所有节点(3) ($poligono是正确的kml文件)

    $xml = simplexml_load_file($poligono) or die("file non caricato");
        $coords   = array();

        foreach($xml->Document->Folder->Placemark->children() as $element)
        {
            foreach($element as $key => $val)
            {
                foreach($xml->Document->Folder->Placemark->LineString as $element)
                {
                    foreach($element as $key => $val)
                    {
                        // echo "{$key}: {$val}" . "<br>";
                        if ({$key}== 'coordinates')
                        {
                          $args     = explode(",", {$val});
                          $coords[] = array($args[0], $args[1], $args[2]);
                        }
                    }
                }
            }
        }

目标是收集数组$ coords []所有坐标,即使它们被细分为不同的子行(谷歌地图kml内的不同节点)。我的错误在哪里? 谢谢你的帮助。

0 个答案:

没有答案