Php 使用Google Maps API v3按名称搜索mySQL数据库

Php 使用Google Maps API v3按名称搜索mySQL数据库,php,mysql,google-maps-api-3,Php,Mysql,Google Maps Api 3,我目前使用谷歌地图API v3创建了一个定位器。它搜索存储在数据库中的位置 这些地点都有名字。我想加入一个搜索功能,它可以按名称或位置搜索数据库。我已经浏览了GooglePlacesAPI,但我不确定在哪里应用这种文本搜索功能。我还没有看到一个很好的例子来说明如何做到这一点,或者这是否可能 如有任何建议、建议或示例,将不胜感激 谢谢 更新:语言-PHP您的pastebin使用了不推荐的。对于新代码,您应该使用或。下面的代码使用PDO try { //Connect to database

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

这些地点都有名字。我想加入一个搜索功能,它可以按名称或位置搜索数据库。我已经浏览了GooglePlacesAPI,但我不确定在哪里应用这种文本搜索功能。我还没有看到一个很好的例子来说明如何做到这一点,或者这是否可能

如有任何建议、建议或示例,将不胜感激

谢谢


更新:语言-PHP

您的pastebin使用了不推荐的。对于新代码,您应该使用。下面的代码使用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; 

用于在未找到记录的情况下在搜索位置提供标记。

我目前使用phpquery设置了它,查询数据库中的名称,并将相应的coords发送到APIsomething,行为:或name='user input name',以便我先查询名称?然后使用该结果作为前往API的查询的名称部分?这里有一个指向我的代码的链接,其中有一个查询@Randy
if ($markerStmt->rowCount()==0) {
...
}