1

我正在构建一个谷歌地图,我想从我的 mysql 数据库中的表中显示各种内容。

我一直在考虑沿着 KML 路线走下去,到目前为止,在某种程度上来说还不错。

我有一个从我当前定位的表中的所有行生成的 KML 文件,但我的问题是,我可以根据数据库中的一行将它们放入“类别”吗?

我现在的代码如下:

<?php
require('dblogin.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 directory WHERE 1';
$result = mysql_query($query);
if (!$result) 
{
die('Invalid query: ' . mysql_error());
}

// Creates an array of strings to hold the lines of the KML file.
$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://earth.google.com/kml/2.1">';
$kml[] = ' <Document>';
$kml[] = ' <Style id="restaurantStyle">';
$kml[] = ' <IconStyle id="restuarantIcon">';
$kml[] = ' <Icon>';
$kml[] = ' <href>http://maps.google.com/mapfiles/kml/pal2/icon63.png</href>';
$kml[] = ' </Icon>';
$kml[] = ' </IconStyle>';
$kml[] = ' </Style>';
$kml[] = ' <Style id="barStyle">';
$kml[] = ' <IconStyle id="barIcon">';
$kml[] = ' <Icon>';
$kml[] = ' <href>http://maps.google.com/mapfiles/kml/pal2/icon27.png</href>';
$kml[] = ' </Icon>';
$kml[] = ' </IconStyle>';
$kml[] = ' </Style>';

// Iterates through the rows, printing a node for each row.
while ($row = @mysql_fetch_assoc($result)) 
{
$kml[] = ' <Placemark id="placemark' . $row['id'] . '">';
$kml[] = ' <name>' . htmlentities($row['name']) . '</name>';
$kml[] = ' <description>' . htmlentities($row['coverage']) . '</description>';
$kml[] = ' <styleUrl>#' . ($row['category']) .'Style</styleUrl>';
$kml[] = ' <Point>';
$kml[] = ' <coordinates>' . $row['lon'] . ','  . $row['lat'] . '</coordinates>';
$kml[] = ' </Point>';
$kml[] = ' </Placemark>';

} 

// End XML file
$kml[] = ' </Document>';
$kml[] = '</kml>';
$kmlOutput = join("\n", $kml);
header('Content-Disposition: attachment; filename="file.kml"');
echo $kmlOutput;
?>

它输出一个可以通过谷歌地图读取的kml文件:https://www.google.com/maps?sll=37.062437029501915%2C-95.7995588577611&sspn=72.35087833291526%2C95.41055226527986&t=m&q=http%3A%2F%2F。 google-boost.co.uk%2Fmapsearch%2Ffile.kml&output=classic&dg=opt

我想要做的是对结果进行某种程度的“排序”,以便在左侧我们根据数据库中的类别行将所有业务分类到类别中。

即所有管道工组合在一起,所有搬迁公司组合在一起等。

今天早上开始玩 KML 路线并走到了这一步,如果有人能进一步了解添加一些结构,那肯定会让我的一天变得更好!

4

0 回答 0