【问题标题】:Google maps v3 - adding php generated markers by user inputGoogle maps v3 - 通过用户输入添加 php 生成的标记
【发布时间】:2011-03-04 18:37:43
【问题描述】:

您好,我正在尝试在 google maps 中制作公交路线查找器。

到目前为止,借助 google 的“将 PHP/MySQL 与 Google Maps 结合使用”示例,我已经在地图上显示了一条静态路线。

这是通过一个 php 文件从数据库中提取数据并制作一个 xml 文件,然后谷歌使用该 xml 数据映射 javascript 来制作标记/折线。

我的版本: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 个条目。

感谢任何指向示例或教程的链接。

【问题讨论】:

    标签: php javascript mysql html xml


    【解决方案1】:

    您需要执行许多步骤。

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

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

     $query = "SELECT * FROM bus109;";
    

    包含特定于数据库结构的 WHERE 子句。 http://www.w3schools.com/sql/sql_where.asp

    3)。您需要在创建 xmlHttpRequest 的 javascript 中设置请求参数

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

    ...如果您选择使用 HTTP Get。

    4)。在表单中或使用 javascript 侦听器创建一个搜索框以允许用户输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多