0

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

我一直在遵循位于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 文件。我只是想不出该怎么做。我已经尝试按照教程的建议进行操作,但是我真的不明白它在做什么,我无法让它工作。开发 php 代码来动态生成 kml 文件后,我认为我已经完成了困难的部分,我觉得我错了。任何建议将不胜感激,因为它非常重要!

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

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

" type="文本/javascript">

            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 文件?尽管有你的帮助,我还是有点困惑。

4

2 回答 2

0

你可能已经完成了最困难的部分。要在 Google 地图上显示 kml 文件,您可以使用KmlLayerapi-doc。请记住,您提供给KmlLayer构造函数的 URL 必须是公开可用的 URL,因此您必须确保能够传递可以从 Web 访问的 URL。KML 是动态生成的很好,您只需使其可供 Google 检索。然后,Google 将处理 kml 并生成图块叠加层。这就是您对以下内容的调用将返回的内容:new google.maps.KmlLayer

如果您想测试您的 URL 是否生成有效的 kml,请查看Galdos KML 验证器;您向它提供一个 URL,它会检索并尝试解析您的 KML。这是一个不错的 kml 烟雾测试实用程序。

于 2012-05-15T18:26:53.643 回答
0

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

于 2016-01-22T17:16:13.650 回答