【问题标题】:Using database with Store Locator on Google Maps API在 Google Maps API 上使用带有 Store Locator 的数据库
【发布时间】:2013-04-02 03:52:43
【问题描述】:

我正在使用 Google Maps API 上的 Store Locator 创建一个系统 - 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),但似乎无法弄清楚如何正确使用它。

有没有人有一个从数据库中提取的例子,因为我正在努力理解它?

【问题讨论】:

    标签: php javascript google-maps google-maps-api-3 mysqli


    【解决方案1】:

    当您想使用数据库时,可以找到正确的示例Here。这个示例的问题是它使用了已弃用的mysql_ 函数。下面的代码使用PDO 而不是phpsqlsearch_genxml.php 中的这些mysql_ 函数

    //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; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多