2013-04-10 257 views
0

我创建与商店定位器上的谷歌地图API的系统使用数据库存储位置 - http://storelocator.googlecode.com/git/index.html?utm_source=geoblog&utm_campaign=sl谷歌地图API

我用这个例子: http://storelocator.googlecode.com/git/examples/dynamic.html

我知道了工作正常,但它是从CSV拉,而我期待从数据库中取而代之。

的JavaScript可以在这里找到,从CSV中提取数据:它具有storeLocator.DataFeed类,但似乎无法找出 http://storelocator.googlecode.com/git/examples/medicare-dynamic-ds.js

我看了一下文档(http://storelocator.googlecode.com/git/reference.html)如何正确使用它。

有没有人有一个这样从数据库中拉出来的例子,因为我正在努力让自己的头靠近它?

回答

1

由于您想使用数据库,因此可以找到正确的示例Here。此示例的问题是它使用已弃用的mysql_函数。下面的代码使用PDO代替这些mysql_函数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;