【问题标题】:Search mySQL database by name using Google Maps API v3使用 Google Maps API v3 按名称搜索 mySQL 数据库
【发布时间】:2013-02-04 14:48:53
【问题描述】:

我目前使用 Google Maps API v3 创建了一个定位器。它搜索存储在数据库中的位置。

这些位置都有名称。我想绑定一个按名称或位置搜索数据库的搜索功能。我查看了 Google Places API,但不确定在哪里应用这种文本搜索功能。我还没有看到一个很好的例子来说明如何做到这一点,或者它是否可能。

任何建议或意见或示例将不胜感激。

谢谢!

更新:语言 - PHP

【问题讨论】:

  • 我目前使用 php 设置它
  • 查询数据库的名称并将对应的坐标发送到API
  • 类似于:OR name ='user input name'
  • 所以我会先查询名字?然后将该结果用作进入 API 的查询的名称部分?这是我的代码的链接,其中包含查询pastebin.com/YRXZG3YX@Randy

标签: php mysql google-maps-api-3


【解决方案1】:

您的 pastebin 使用已弃用的 mysql_ extension。对于新代码,您应该使用 MySQLiPDO。以下代码使用 PDO

try {
    //Connect to database
    $dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    // Prepare statement 1
    $searchStmt = $dbh->prepare("SELECT lat,lng FROM markers WHERE name LIKE ? ");
    // Assign parameters
    $searchStmt->bindParam(1,$name);
    $searchStmt->setFetchMode(PDO::FETCH_ASSOC);
    $searchStmt->execute();
    $row = $searchStmt->fetch();
    $center_lat = $row['lat'];
    $center_lng = $row['lng'];
    // Start XML file, create parent node
    $dom = new DOMDocument("1.0");
    header("Content-type: text/xml");
    $node = $dom->createElement("markers");
    $parnode = $dom->appendChild($node);
    // Prepare statement 2
    $markerStmt = $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 markers HAVING distance < ? ORDER BY distance LIMIT 0 , 20");
    // Assign parameters
    $markerStmt->bindParam(1,$center_lat);
    $markerStmt->bindParam(2,$center_lng);
    $markerStmt->bindParam(3,$center_lat);
    $markerStmt->bindParam(4,$radius);
    //Execute query
    $markerStmt->setFetchMode(PDO::FETCH_ASSOC);
    $markerStmt->execute();
    //Default for zero rows
    if ($markerStmt->rowCount()==0) {
        $node = $dom->createElement("marker");
        $newnode = $parnode->appendChild($node);
        $newnode->setAttribute("name", "No Records Found");
        $newnode->setAttribute("lat", $center_lat);
        $newnode->setAttribute("lng", $center_lng);
        $newnode->setAttribute("distance", 0);
                }
    else {
    // Iterate through the rows, adding XML nodes for each
    while($row = $markerStmt->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]').", mapSelect.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }
//Close the connection
$dbh = null; 

$searchStmt 在数据库中搜索名称。它使用为$markerStmt 找到的坐标。

if ($markerStmt->rowCount()==0) {
...
}

如果没有找到记录,则用于在搜索位置提供标记。

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多