使用php从mysql数据库创建kml文件 [英] Creating a kml file from a mysql database with php

查看:90
本文介绍了使用php从mysql数据库创建kml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

b_id(主键)
b_name
hrepparid(不需要显示)
stjarna(无需显示)
lat
lng
评论

该表包含一个农场的名称(b_name),其地理坐标(lat,lng)和注释-如果有的话.

我需要将这些数据转换为kml文件.我试图浏览Google网页上的教程,但是一旦尝试编辑,它就可以工作了,它转到h ***.据我了解,kml文件基本上是一个xml文件,但是不幸的是我的php技能还不足以解决这个问题.

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

I hope there is a php genius here somewhere who can help me with this. I have a database table called baeir, it has the following fields:

b_id (primary key)
b_name
hrepparid (does not need to be displayed)
stjarna (does not need to be displayed)
lat
lng
comments

The table contains a name of a farm (b_name), its geographical coordinates (lat, lng) and comments - if there are any.

What I need is to get this data into a kml file. I have tried to go through the tutorial on google's webpage and I got it to work BUT once I try to edit, it goes to h***. As far as I can understand, a kml file is basically an xml file but unfortunately my php skills are not advanced enough to handle this.

I hope someone can help :-))

这是我从Google那里获得的代码,它看起来也有点...按我的喜好进行了详细说明.有什么想法可以简化代码,以便我可以将变量放入其中,而别无其他?

This is the code I have from google, and it looks a bit too...detailed for my liking. Any ideas how to simplify the code so that I can get my variables into it and nothing else?

<?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;

?>

推荐答案

如上所述,以下Google Maps教程完全回答了您的问题ALMOST:

as mentioned above, the following Google Maps tutorial answers your question ALMOST completely: http://code.google.com/apis/kml/articles/phpmysqlkml.html

不幸的是,它没有提出用于多边形分析的代码(如果要管理农场区域显示,则需要该代码),但是您可以调整LinesString解析方法并实现它.谨防将外部边界标签正确地嵌入到多边形标签中,并记住您必须复制要正确绘制的多边形的起点.

unfortunately, it does not propose code for polygon parsing (which you will need, if you are managing farm areas display), but you can adapt the LinesString parsing method and achieve it. beware of properly embedding the outerBoundaryIs tag in the Polygon tag AND remember that you must duplicate the starting point for the polygon to be drawn correctly.

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

这篇关于使用php从mysql数据库创建kml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆