如何将动态生成的kml / xml文件加载到嵌入式Google地图中

时间:2012-05-15 18:18:14

标签: google-maps google-maps-api-3

你好对这个问题的任何建议都会非常感激,因为它似乎应该是一个简单的问题,但它给我带来了很大的困难。

我一直在关注位于https://developers.google.com/maps/articles/phpsqlajax的“使用PHP / MySQL与Google地图”教程生成kml / xml文件。我生成的kml文件与示例中看到的文件的不同之处在于,我生成了一系列建筑物的多边形边界,而不仅仅是在特定位置创建地点标记。此页面的代码如下所示......

// Creates the Document.
    $dom = new DOMDocument('1.0', 'UTF-8');

    // Creates the root KML element and appends it to the root document.
    $node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
    $parNode = $dom->appendChild($node);

    // Creates a KML Document element and append it to the KML element.
    $dnode = $dom->createElement('Document');
    $docNode = $parNode->appendChild($dnode);

    //Style 1
    //Set Style ID
    $restStyleNode = $dom->createElement('Style');
    $restStyleNode->setAttribute('id', '#style1');
    $restIconstyleNode = $dom->createElement('IconStyle');
    $restIconstyleNode->setAttribute('id', 'BuildingIcon');

    //Set LineStyle
    $restIconNode = $dom->createElement('LineStyle');
    $restHref = $dom->createElement('colour', 'FF990000');
    $restIconNode->appendChild($restHref);
    $restWidth = $dom->createElement('width', '2');
    $restIconNode->appendChild($restWidth);
    $restIconstyleNode->appendChild($restIconNode);
    $restStyleNode->appendChild($restIconstyleNode);
    $docNode->appendChild($restStyleNode);

    //Set Polystyle
    $restIconNode = $dom->createElement('PolyStyle');
    $restHref = $dom->createElement('colour', 'FFCC0000');
    $restIconNode->appendChild($restHref);
    $restFill = $dom->createElement('fill', '1');
    $restIconNode->appendChild($restFill);
    $restOutline = $dom->createElement('outline', '1');
    $restIconNode->appendChild($restOutline);
    $restIconstyleNode->appendChild($restIconNode);
    $restStyleNode->appendChild($restIconstyleNode);
    $docNode->appendChild($restStyleNode);

    //Style 2
    //Set Style ID
    $restStyleNode = $dom->createElement('Style');
    $restStyleNode->setAttribute('id', '#style2');
    $restIconstyleNode = $dom->createElement('IconStyle');
    $restIconstyleNode->setAttribute('id', 'HighfieldIcon');

    //Set LineStyle
    $restIconNode = $dom->createElement('LineStyle');
    $restHref = $dom->createElement('colour', 'ff000000');
    $restIconNode->appendChild($restHref);
    $restWidth = $dom->createElement('width', '2');
    $restIconNode->appendChild($restWidth);
    $restIconstyleNode->appendChild($restIconNode);
    $restStyleNode->appendChild($restIconstyleNode);
    $docNode->appendChild($restStyleNode);

    //Set Polystyle
    $restIconNode = $dom->createElement('PolyStyle');
    $restHref = $dom->createElement('colour', 'ff000000');
    $restIconNode->appendChild($restHref);
    $restFill = $dom->createElement('fill', '1');
    $restIconNode->appendChild($restFill);
    $restOutline = $dom->createElement('outline', '1');
    $restIconNode->appendChild($restOutline);
    $restIconstyleNode->appendChild($restIconNode);
    $restStyleNode->appendChild($restIconstyleNode);
    $docNode->appendChild($restStyleNode);

    // Iterates through the MySQL results, creating one Placemark for each row.
    while ($row = @mysql_fetch_assoc($result))
        {
            // Creates a Placemark and append it to the Document.

            $node = $dom->createElement('Placemark');
            $placeNode = $docNode->appendChild($node);

            // Creates an id attribute and assign it the value of id column.
            $placeNode->setAttribute('id', 'placemark' . $row['buildingID']);

              // Create name, and description elements and assigns them the values of the name and address columns from the results.
              $nameNode = $dom->createElement('name',htmlentities($row['name']));
              $placeNode->appendChild($nameNode);
              $styleUrl = $dom->createElement('styleUrl', $row['styleID']);
              $placeNode->appendChild($styleUrl);

              // Creates a Polygon element.
              $polygonNode = $dom->createElement('Polygon');
              $placeNode->appendChild($polygonNode);

              //Add extrude information
              $restExtrude = $dom->createElement('extrude', '1');
              $polygonNode->appendChild($restExtrude);

              //Add altitude information
              $restAltitude = $dom->createElement('altitudeMode', 'relativeToGround');
              $polygonNode->appendChild($restAltitude);

              //Add outerBoundaryIsNode
              $outerBoundaryIsNode = $dom->createElement('outerBoundaryIs');
              $polygonNode->appendChild($outerBoundaryIsNode);

              //Add LinearRing Node
              $LinearRingNode = $dom->createElement('LinearRing');
              $outerBoundaryIsNode->appendChild($LinearRingNode);

              //Adding information into linear ring node
              $tessellate = $dom->createElement('tessellate', '1');
              $LinearRingNode->appendChild($tessellate);

              // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
              $coorStr = $row['longitude1'] . ','  . $row['latitude1'] . ' ' . $row['longitude2'] . ',' . $row['latitude2'] . ' ' . $row['longitude3'] . ',' . $row['latitude3'] . ' ' . $row['longitude4'] . ',' . $row['latitude4'] . ' ' . $row['longitude5'] . ',' . $row['latitude5'];
              $coorNode = $dom->createElement('coordinates', $coorStr);
              $LinearRingNode->appendChild($coorNode);
        }

        $kmlOutput = $dom->saveXML();
        header('Content-type: application/vnd.google-earth.kml+xml');
        echo $kmlOutput;

我已经检查了通过将其加载到地图中生成的kml文件,我对结果很满意,但是我不能为我的生活弄清楚如何让我的网站显示我的php文件生成的任何kml文件。我只是想不出怎么做。我已经尝试过按照教程的建议,但是我真的不明白它在做什么,我无法让它工作。已经开发了动态生成kml文件的PHP代码,我认为我做了很多困难,我觉得我错了。任何建议都会非常感激,因为它非常重要!

目前我正在使用以下代码:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=key

”             类型= “文本/ JavaScript的” &GT;              

            var map;
            var geoXml; 
            var toggleState = 1;

    function initialize() 
    {
        if (GBrowserIsCompatible())
            {
                geoXml = new GGeoXml("Locationoffile.part");
                map = new GMap2(document.getElementById("map_canvas")); 
                map.setCenter(new GLatLng(50.xxxxx,-1.xxxxxxx), 16); 
                map.addControl(new GLargeMapControl());
                 map.addOverlay(geoXml);
            }
    } 


function toggleMyKml() 
    {
    if (toggleState == 1) 
        {
        map.removeOverlay(geoXml);
        toggleState = 0;
        } 
    else 
        {
        map.addOverlay(geoXml);
        toggleState = 1;
        }
    }

通过将php生成的kml文件中的输出复制到静态位置来生成映射。这有效。我需要编辑多少才能让它加载我的php文件呢?尽管你有帮助,我仍然有点困惑。

2 个答案:

答案 0 :(得分:0)

你可能已经做了很多困难。要在Google地图上显示kml文件,您可以使用KmlLayer api-doc。请注意,您提供给KmlLayer构造函数的网址必须是公开的网址,因此您必须确保能够传递可从网络上访问的网址。 KML是动态生成的,你必须让它可供谷歌检索。然后,Google将处理kml并生成图块叠加层。这就是您致电new google.maps.KmlLayer的回复。

如果您想测试您的网址是否产生有效的kml,请查看Galdos KML Validator;你提供了一个URL,它将检索并尝试解析你的KML。这是一个很好的kml烟雾测试实用程序。

答案 1 :(得分:0)

可能存在问题。 Google会缓存kml文​​件。每次制作新版本时,您都需要更改已保存的kml文件名。如果没有这个,您将继续使用旧的缓存版本而不是最新版本。