2011-03-04 86 views
0

您好,我正在尝试在谷歌地图中制作一个公交路线查找器。谷歌地图v3 - 通过用户输入添加php生成的标记

到目前为止,我已经有了一个静态路线,可以在谷歌的“使用PHP/MySQL和谷歌地图”的帮助下在地图上显示。

这工作的PHP文件从数据库中提取数据并制作一个XML文件,然后谷歌映射JavaScript使用该XML数据制作标记/多段线。

我的版本:http://www.ykothari.co.uk/gmap-php/gmap-php.html

PHP代码:

<?php 
require("dbserverinfo.php"); 

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a MySQL server 
$connection=mysql_connect ($dbserver, $username, $password); 
if (!$connection) { 
    die('Not connected : ' . mysql_error()); 
} 

// Set the active MySQL database 
$db_selected = mysql_select_db($database, $connection); 
if (!$db_selected) { 
    die ('Can\'t use db : ' . mysql_error()); 
} 
// Select all the rows in the markers table 
$query = "SELECT * FROM bus109;"; 
$result = mysql_query($query); 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 

header("Content-type: text/xml"); 

// Start XML file, echo parent node 
echo '<?xml version="1.0"?>' . "\n"; 
echo '<markers>' . "\n"; 

// Iterate through the rows, printing XML nodes for each 
while ($row = @mysql_fetch_assoc($result)){ 
    // ADD TO XML DOCUMENT NODE 
    echo '<markers109 '; 
    echo 'stopname="' . parseToXML($row['stopname']) . '" '; 
    echo 'lat="' . $row['lat'] . '" '; 
    echo 'lng="' . $row['lng'] . '" '; 
    echo 'service="' . $row['service'] . '" '; 
    echo 'stopid="' . $row['stopid'] . '" '; 
    echo '/>' . "\n"; 
} 
// End XML file 
echo '</markers>'; 
?> 

存储数据是这样的:https://spreadsheets.google.com/ccc?key=0AqEGwIC84XiqdGZXM2VMS1VPazdrVExsa0t6TjhPWlE&hl=en&authkey=CMSJup8M

现在我想添加一个搜索框供用户在巴士路线号码类型他们希望看到,所以当他们在搜索框中输入“1”时,php脚本只会获取该路由器的标记。我假设php文件中的“select * from table”正在获取数据。有没有一种方法可以在用户输入时修改此行,或者有其他方法来实现此目的?因此,该文件只能获取该路线的行数据,而不是整个数据库作为数据库的约50,000条目。

赞赏任何指向示例或教程的链接。

回答

0

您需要执行多个步骤。

1)。修改您的php文件以接受具有所需特定信息的GET或POST请求参数。请参阅此链接了解更多信息:http://us.php.net/manual/en/reserved.variables.get.php

2)。您需要修改SQL语句:

$query = "SELECT * FROM bus109;"; 

要包含特定于数据库结构的WHERE子句。 3)。3)。你需要在你的javascript中设置请求参数,你在哪里制作xmlHttpRequest

downloadUrl('xmlgen.php?bus_route_id=1&....' 

......如果你选择使用HTTP Get。 4)。制作一个搜索框在一个窗体或一个JavaScript侦听器允许用户输入。