在Google Maps API上使用Store Locator的数据库 [英] Using database with Store Locator on Google Maps API

查看:154
本文介绍了在Google Maps API上使用Store Locator的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google地图API中的商店定位器创建系统 -
http://storelocator.googlecode.com/git/index.html?utm_source=geoblog&utm_campaign=sl



我正在使用此示例:
http://storelocator.googlecode.com/git/ examples / dynamic.html



我的工作正常,但它从CSV中拉出来,而我正在从数据库中取而代之。

javascript可以在这里找到,从CSV中提取数据:
http://storelocator.googlecode.com/git/examples/medicare-dynamic-ds.js



我看了一下文档( http://storelocator.googlecode.com/git/reference.html ),它有storeLocator.DataFeed类,但似乎无法弄清楚如何正确使用它。 / p>

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

解决方案

当您想使用数据库时,可以找到正确的示例 Here 。这个例子的问题是它使用了不赞成的 mysql _ 函数。下面的代码使用 phpsqlsearch_genxml.php



  //连接数据库
$ dbh = new PDO(mysql:host = $ host; dbname = $ database, $ username,$ password);
$ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
try {
//准备语句
$ stmt = $ dbh-> prepare(SELECT name,lat,lng,(3959 * acos(cos(radians(?))* cos (弧度(lat))* cos(弧度(lng) - 弧度(θ))+ sin(弧度(θ))* sin(弧度(lat))))AS距离FROM表距离<距离ORDER BY LIMIT 0,20);
//赋值参数
$ stmt-> bindParam(1,$ center_lat);
$ stmt-> bindParam(2,$ center_lng);
$ stmt-> bindParam(3,$ center_lat);
$ stmt-> bindParam(4,$ radius);
//执行查询
$ stmt-> setFetchMode(PDO :: FETCH_ASSOC);
$ stmt-> execute();
header(Content-type:text / xml);
//迭代遍历行,为每个
添加XML节点,同时($ row = $ stmt-> fetch()){
$ node = $ dom-> createElement(标记);
$ 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对不起,我担心你不能那样做。 $ e-> getMessage(); //在测试
file_put_contents('PDOErrors.txt',date('[Ymd H:i:s]'),phpsqlsearch_genxml.php后删除或修改。 $ e-> getMessage()。\r\\\
,FILE_APPEND);
}
//关闭连接
$ dbh = null;


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?

解决方案

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; 

这篇关于在Google Maps API上使用Store Locator的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆