我正在使用Google Maps API上的商店定位器创建系统 - http://storelocator.googlecode.com/git/index.html?utm_source=geoblog&utm_campaign=sl
我使用的是这个例子: http://storelocator.googlecode.com/git/examples/dynamic.html
我已经让它工作正常了,但它正在从CSV中提取,而我正在寻求从数据库中取出它。
可在此处找到从CSV中提取数据的javascript: http://storelocator.googlecode.com/git/examples/medicare-dynamic-ds.js
我已经查看了具有storeLocator.DataFeed类的文档(http://storelocator.googlecode.com/git/reference.html),但似乎无法弄清楚如何正确使用它。
有没有人从数据库中获取这个例子,因为我正在努力解决这个问题?
答案 0 :(得分: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;