Using database with Store Locator on Google Maps A

2019-06-09 07:08发布

问题:

I'm creating a system with the Store Locator on Google Maps API - http://storelocator.googlecode.com/git/index.html?utm_source=geoblog&utm_campaign=sl

I was using this example: http://storelocator.googlecode.com/git/examples/dynamic.html

I've got it working fine but it's pulling from a CSV whereas I'm looking to get it pulling from a database instead.

The javascript can be found here that extract the data from CSV: http://storelocator.googlecode.com/git/examples/medicare-dynamic-ds.js

I've had a look at the documentation (http://storelocator.googlecode.com/git/reference.html) which has the storeLocator.DataFeed class but can't seem to figure out how to use it correctly.

Does anyone have an example of this pulling from a database as i'm struggling to get my head around it?

回答1:

As you want to use a database the correct example can be found Here.The problem with this example is it uses the deprecated mysql_ functions. The code below uses PDO instead of these mysql_ functions in phpsqlsearch_genxml.php

//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
// Prepare statement
$stmt = $dbh->prepare("SELECT  name, lat, lng, ( 3959 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance FROM table HAVING distance < ? ORDER BY distance LIMIT 0 , 20");
// Assign parameters
$stmt->bindParam(1,$center_lat);
$stmt->bindParam(2,$center_lng);
$stmt->bindParam(3,$center_lat);
$stmt->bindParam(4,$radius);
//Execute query
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while($row = $stmt->fetch()) {
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("name", $row['name']);
    $newnode->setAttribute("lat", $row['lat']);
    $newnode->setAttribute("lng", $row['lng']);
    $newnode->setAttribute("distance", $row['distance']);
    }
echo $dom->saveXML();
 }
 catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing 
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", phpsqlsearch_genxml.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }
//Close the connection
$dbh = null;