2011-03-04 91 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偵聽器允許用戶輸入。