开发者

Google maps v3 - adding php generated markers by user input

Hello i'm trying to make a bus route finder in google maps.

So far i've got a static route to display on the map with the help of google's "Using PHP/MySQL with Google Maps" example.

That works by a php file extracting data from a database and making a xml file, and then the google maps javascript using that xml data to make markers/polyline.

My version: http://www.ykothari.co.uk/gmap-php/gmap-php.html

php code:

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

The dataset is like this: https://spreadsheets.google.com/ccc?key=0AqEGwIC84XiqdGZXM2VMS1VPazdrVExsa0t6TjhPWlE&hl=en&authkey=CMSJup8M

Now i want to add a search box for user to type in the bus route number that they want to see, so when they type in say "1" in the search box the php script only gets markers for that routenumber. I assume that the "select * from table" in the php file, is getting the data. Is there a way i can modify this line as the user types it or is there any other way to achieve this? So that the file only fetches the row data for that route and not the entire database as the database about 50,000 entries.

Any links to examples or tutorials are appreciated.


There are a number of steps you'll need to do.

1). Modify your php file to accept GET or POST request parameters with the specific information desired. See this link for more info: http://us.php.net/manual/en/reserved.variables.get.php

2). You'll need to modify the SQL statement:

 $query = "SELECT * FROM bus109;";

to include a WHERE clause specific to the structure of your database. http://www.w3schools.com/sql/sql_where.asp

3). You'll need to set the request parameters in your javascript where you make the xmlHttpRequest

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

...if you choose to use an HTTP Get.

4). Make a searchbox either in a form or with a javascript listener to allow for user input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜