【问题标题】:Creating a kml file from a mysql database with php使用 php 从 mysql 数据库创建 kml 文件
【发布时间】:2012-01-23 20:42:14
【问题描述】:

我希望这里有一个 php 天才可以帮助我解决这个问题。我有一个名为 baeir 的数据库表,它有以下字段:

b_id(主键)
b_name
hrepparid(不需要显示)
stjarna(不需要显示)
纬度


cmets

该表包含农场的名称 (b_name)、其地理坐标 (lat, lng) 和 cmets - 如果有的话。

我需要的是把这些数据放到一个 kml 文件中。我试图通过谷歌网页上的教程,我让它工作但是一旦我尝试编辑,它就会进入 h***。据我所知,kml 文件基本上是一个 xml 文件,但不幸的是我的 php 技能还不够先进,无法处理这个问题。

我希望有人可以提供帮助:-))

这是我从谷歌获得的代码,它看起来有点太……根据我的喜好详细说明。有什么想法可以简化代码以便我可以将变量放入其中而不是其他任何东西?

<?php
require('phpsqlajax_dbinfo.php');

// Opens a connection to a MySQL server.

$connection = mysql_connect ($server, $username, $password);

if (!$connection) 
{
  die('Not connected : ' . mysql_error());
}
// Sets the active MySQL database.
$db_selected = mysql_select_db($database, $connection);

if (!$db_selected) 
{
  die('Can\'t use db : ' . mysql_error());
}

// Selects all the rows in the markers table.
$query = 'SELECT * FROM markers WHERE 1';
$result = mysql_query($query);

if (!$result) 
{
  die('Invalid query: ' . mysql_error());
}

// 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);


// Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element.
$restStyleNode = $dom->createElement('Style');
$restStyleNode->setAttribute('id', 'restaurantStyle');
$restIconstyleNode = $dom->createElement('IconStyle');
$restIconstyleNode->setAttribute('id', 'restaurantIcon');
$restIconNode = $dom->createElement('Icon');
$restHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
$restIconNode->appendChild($restHref);
$restIconstyleNode->appendChild($restIconNode);
$restStyleNode->appendChild($restIconstyleNode);
$docNode->appendChild($restStyleNode);

$barStyleNode = $dom->createElement('Style');
$barStyleNode->setAttribute('id', 'barStyle');
$barIconstyleNode = $dom->createElement('IconStyle');
$barIconstyleNode->setAttribute('id', 'barIcon');
$barIconNode = $dom->createElement('Icon');
$barHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
$barIconNode->appendChild($barHref);
$barIconstyleNode->appendChild($barIconNode);
$barStyleNode->appendChild($barIconstyleNode);
$docNode->appendChild($barStyleNode);

// 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['id']);

  // 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);
  $descNode = $dom->createElement('description', $row['address']);
  $placeNode->appendChild($descNode);
  $styleUrl = $dom->createElement('styleUrl', '#' . $row['type'] . 'Style');
  $placeNode->appendChild($styleUrl);

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

  // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
  $coorStr = $row['lng'] . ','  . $row['lat'];
  $coorNode = $dom->createElement('coordinates', $coorStr);
  $pointNode->appendChild($coorNode);
}


$kmlOutput = $dom->saveXML();
while (@ob_end_clean());
header('content-type:text/xml;');
echo $kmlOutput;

?>

【问题讨论】:

    标签: php mysql kml


    【解决方案1】:

    如上所述,以下 Google 地图教程几乎完全回答了您的问题:http://code.google.com/apis/kml/articles/phpmysqlkml.html

    不幸的是,它没有提出用于多边形解析的代码(如果您正在管理农场区域显示,您将需要它),但您可以调整 LinesString 解析方法并实现它。注意不要在 Polygon 标记中正确嵌入 outerBoundaryIs 标记,记住必须复制起点才能正确绘制多边形。

        $lineNode = $dom->createElement('Polygon');
    $placeNode = $placeNode->appendChild($lineNode);
    $exnode = $dom->createElement('extrude', '1');
    $lineNode->appendChild($exnode);
    $almodenode =$dom->createElement(altitudeMode,'relativeToGround');
    $lineNode->appendChild($almodenode);
    $outerboundnode = $dom->createElement('outerBoundaryIs');
    $placeNode = $placeNode->appendChild($outerboundnode);
    $ringnode =$dom->createElement('LinearRing');
    $placeNode = $placeNode->appendChild($ringnode);
        // optional styletag colors the polygon
    //$stylenode =$dom->createElement(styleUrl,'#transYellowPoly');
    //$lineNode->appendChild($stylenode);
    
    //Create a coordinates element and give it the value of the lng and lat columns from the results
    //$coorNode = $dom->createElement('coordinates',$row['coordinates']);
    $coorNode = $dom->createElement('coordinates',$coordinates);
    $placeNode = $placeNode->appendChild($coorNode);
    

    【讨论】:

    • 在您投入时间开发使用它的应用程序之前,不要忘记 GOOGLMAPS API 不是免费的。
    【解决方案2】:

    试试这个:

    <?php
    require('phpsqlajax_dbinfo.php');
     function xmlEntities($str) 
    { 
        $xml = array('&#34;','&#38;','&#38;','&#60;','&#62;','&#160;','&#161;','&#162;','&#163;','&#164;','&#165;','&#166;','&#167;','&#168;','&#169;','&#170;','&#171;','&#172;','&#173;','&#174;','&#175;','&#176;','&#177;','&#178;','&#179;','&#180;','&#181;','&#182;','&#183;','&#184;','&#185;','&#186;','&#187;','&#188;','&#189;','&#190;','&#191;','&#192;','&#193;','&#194;','&#195;','&#196;','&#197;','&#198;','&#199;','&#200;','&#201;','&#202;','&#203;','&#204;','&#205;','&#206;','&#207;','&#208;','&#209;','&#210;','&#211;','&#212;','&#213;','&#214;','&#215;','&#216;','&#217;','&#218;','&#219;','&#220;','&#221;','&#222;','&#223;','&#224;','&#225;','&#226;','&#227;','&#228;','&#229;','&#230;','&#231;','&#232;','&#233;','&#234;','&#235;','&#236;','&#237;','&#238;','&#239;','&#240;','&#241;','&#242;','&#243;','&#244;','&#245;','&#246;','&#247;','&#248;','&#249;','&#250;','&#251;','&#252;','&#253;','&#254;','&#255;');
        $html = array('&quot;','&amp;','&amp;','&lt;','&gt;','&nbsp;','&iexcl;','&cent;','&pound;','&curren;','&yen;','&brvbar;','&sect;','&uml;','&copy;','&ordf;','&laquo;','&not;','&shy;','&reg;','&macr;','&deg;','&plusmn;','&sup2;','&sup3;','&acute;','&micro;','&para;','&middot;','&cedil;','&sup1;','&ordm;','&raquo;','&frac14;','&frac12;','&frac34;','&iquest;','&Agrave;','&Aacute;','&Acirc;','&Atilde;','&Auml;','&Aring;','&AElig;','&Ccedil;','&Egrave;','&Eacute;','&Ecirc;','&Euml;','&Igrave;','&Iacute;','&Icirc;','&Iuml;','&ETH;','&Ntilde;','&Ograve;','&Oacute;','&Ocirc;','&Otilde;','&Ouml;','&times;','&Oslash;','&Ugrave;','&Uacute;','&Ucirc;','&Uuml;','&Yacute;','&THORN;','&szlig;','&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&divide;','&oslash;','&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&thorn;','&yuml;');
        $str = str_replace($html,$xml,$str); 
        $str = str_ireplace($html,$xml,$str); 
        return $str; 
    } 
    
    // Opens a connection to a MySQL server.
    
    $connection = mysql_connect ($server, $username, $password);
    
    if (!$connection) 
    {
      die('Not connected : ' . mysql_error());
    }
    // Sets the active MySQL database.
    $db_selected = mysql_select_db($database, $connection);
    
    if (!$db_selected) 
    {
      die('Can\'t use db : ' . mysql_error());
    }
    
    // Selects all the rows in the markers table.
    $query = 'SELECT * FROM baeir WHERE 1';
    $result = mysql_query($query);
    
    if (!$result) 
    {
      die('Invalid query: ' . mysql_error());
    }
    
    // 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);
    
    
    // Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element.
    $restStyleNode = $dom->createElement('Style');
    $restStyleNode->setAttribute('id', 'restaurantStyle');
    $restIconstyleNode = $dom->createElement('IconStyle');
    $restIconstyleNode->setAttribute('id', 'restaurantIcon');
    $restIconNode = $dom->createElement('Icon');
    $restHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
    $restIconNode->appendChild($restHref);
    $restIconstyleNode->appendChild($restIconNode);
    $restStyleNode->appendChild($restIconstyleNode);
    $docNode->appendChild($restStyleNode);
    
    $barStyleNode = $dom->createElement('Style');
    $barStyleNode->setAttribute('id', 'barStyle');
    $barIconstyleNode = $dom->createElement('IconStyle');
    $barIconstyleNode->setAttribute('id', 'barIcon');
    $barIconNode = $dom->createElement('Icon');
    $barHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
    $barIconNode->appendChild($barHref);
    $barIconstyleNode->appendChild($barIconNode);
    $barStyleNode->appendChild($barIconstyleNode);
    $docNode->appendChild($barStyleNode);
    
    // 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['b_id ']);
    
      // Create name, and description elements and assigns them the values of the name and address columns from the results.
      $nameNode = $dom->createElement('name',xmlEntities(htmlentities($row['b_name'])));
      $placeNode->appendChild($nameNode);
      $descNode = $dom->createElement('description', $row['comments']);
      $placeNode->appendChild($descNode);
      //$styleUrl = $dom->createElement('styleUrl', '#' . $row['type'] . 'Style');
      //$placeNode->appendChild($styleUrl);
    
      // Creates a Point element.
      $pointNode = $dom->createElement('Point');
      $placeNode->appendChild($pointNode);
    
      // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
      $coorStr = $row['lng'] . ','  . $row['lat'];
      $coorNode = $dom->createElement('coordinates', $coorStr);
      $pointNode->appendChild($coorNode);
    }
    
    
    $kmlOutput = $dom->saveXML();
    while (@ob_end_clean());
    header('content-type:text/xml;');
    echo $kmlOutput;
    
    ?>
    

    【讨论】:

    • 此页面包含以下错误:第 2 行第 292 列的错误:未定义实体“Aacute”
    • 我删除了 htmlentities,请重试。
    • 对不起,我仍然收到错误:此页面包含以下错误:第 2 行第 284 列的错误:编码错误
    • 在这里查看:hafdal.dk/kml/phpsql_genkml_new1.php
    • 好的,这些实体在这里引起了问题,并且 XML 不支持所有的 HTML 实体。请再试一次,我添加了将html实体转换为XML实体的功能
    【解决方案3】:

    kml 与您的 php.ini 无关。您的 kml 文件基本上是一个 xml 文件,专门用于 google earth ang google map 应用程序。您的 Kml 文件需要通过有效性测试才能使用它。您可以使用以下链接来验证您的 kml 文件。 Kml validator。您的 kml 文件将包含预定义的标签,例如 point coordinate description 以便可以对其进行解析。如果您想查看 kml 文件的外观,请查看

    Sample Kml File

    不用说您的 KML 应该在正确的标签中包含信息,然后您需要对其进行解析。 e-g lat 和 lng 可以进入坐标标签。描述标签可以保存你的文字描述,比如评论。

    【讨论】:

    • 好吧,我可能表述错了,但我需要一个 php 文件来从 mysql 数据库生成 kml 文件...我知道 kml 与 php 没有直接关系,但我也知道你可以用php生成一个kml文件,这就是我所需要的。
    • 不幸的是我不是一个 php 人,但我认为这个链接应该可以帮助你,如果你还没有看到这个:code.google.com/apis/kml/articles/phpmysqlkml.html
    • 感谢 refhat,我已经尝试过了,但是一旦我编辑它,我就无法让它工作。
    • 您能否报告您可能遇到的错误。我只是说因为 kml 非常严格,并使用我的 kmlvalidator 进行验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2020-03-01
    • 2012-04-23
    • 2012-08-30
    • 1970-01-01
    • 2013-08-30
    相关资源
    最近更新 更多