1

我已经使用谷歌地理编码 v3 大约 6 个月了,但它突然停止工作(我收到 610 错误)。它仅在上周左右停止。

然后我遇到了这个(见页面顶部的粉红色框!):https ://developers.google.com/maps/documentation/geocoding/v2/

我已经阅读了所有文档,但不知道从哪里开始!

我希望这是一个小小的改变,因为它需要很长时间才能做到这一点,有人可以帮忙吗?

[在此处查看完整网站][1]


更新:

require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("teamwork_poh", $con);


    $company = get_the_title();
    $address = get_field('address_line_1');
    $city = get_field('town_/_city');
    $post_code = get_field('post_code');
    $link = get_permalink();
    $type = get_field('kind_of_organisation');

    $sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
    $row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
    if ($row_dup['cnt'] == 0) {
        mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`, `link`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '".$type."', '".$link."')");
    }
wp_reset_query();

require("database.php");

define("MAPS_HOST", "maps.googleapis.com");
define("KEY", "(my key)");

// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die("Can\'t use db : " . mysql_error());
}

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

//Initialize delay in geocode speed

$delay=0;
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/json?address=";

while ($row = @mysql_fetch_assoc($result)) {
    if (!($row['lat'] * 1)) {
        $geocode_pending = true;

    while ($geocode_pending){

    $address = $row["address"];
    $id = $row["id"];

    $request_url = $base_url . "" . urlencode($address) ."&sensor=false";

    sleep(0.1);

$json = file_get_contents($request_url);
$json_decoded = json_decode($json);

$status = $json_decoded->status;

 if (strcmp($json_decoded->status, "OK") == 0) {

$geocode_pending = false;

      $lat = $json_decoded->results[0]->geometry->location->lat;
      $lng = $json_decoded->results[0]->geometry->location->lng;

// echo 'here';

      $query = sprintf("UPDATE markers " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
      $update_result = mysql_query($query);

      echo $id;

      if (!$update_result) {
        die("Invalid query: " . mysql_error());
      }
    }

   else {
 // failure to geocode
      $geocode_pending = false;
      echo "Address " . $address . " failed to geocode. ";
      echo "Received status " . $status . "\n";
    }

  //  usleep($delay);

    }
}

}
4

2 回答 2

4

当您将坐标存储在数据库中时,最好在插入新记录时进行地理编码。IE

require("dbinfo.php");//Your database parameters
//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
    //Prepare query
    $name = "%".$company."%";//Wildcard for PDO paramerter
    $countSql = "SELECT COUNT(*) FROM markers WHERE `name` LIKE ?"; 
    $countStmt = $dbh->prepare($countSql);
    // Assign parameter
    $countStmt->bindParam(1,$name);
    //Execute query
    $countStmt->execute();  
    // check the row count  
    if ($countStmt->fetchColumn() == 0) { #1 EDIT changed >0 to ==0
        echo "No row matched the query."; //EDIT  From Row
        $q =$address.','.$city.','.$post_code.',UK'; 
        echo "\n";
        $base_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
        $request_url = $base_url.urlencode($q)."&sensor=false";
        $xml = simplexml_load_file($request_url) or die("url not loading");
        if($xml->status=="OK"){#2
            // Successful geocode
            $lat = $xml->result->geometry->location->lat;
            $lng = $xml->result->geometry->location->lng; 
            $insertSql ="INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`, `link`) VALUES (?,?,?,?,?,?)";
            $insertStmt = $dbh->prepare($insertSql);
            // Assign parameter
            $insertStmt->bindParam(1,$company);
            $insertStmt->bindParam(2,$address); 
            $insertStmt->bindParam(3,$lat);
            $insertStmt->bindParam(4,$lng);
            $insertStmt->bindParam(5,$type);
            $insertStmt->bindParam(6,$link);
            //Execute query
            $insertStmt->execute();
        } #2
        else{
            "No rows inserted."; 
        }#2
    } #1
    else {#1
    echo "Rows matched the query."; //EDIT From No row
    } #1 
}// End try 


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]').", myFile.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }

我已将您的代码转换为 PDO,因为建议停止使用mysql_函数,因为这些函数已被弃用。

我已经让您实现如何处理不返回坐标的地理编码。您还可以检查和处理以下状态码

OVER_QUERY_LIMIT

零结果

请求被拒绝

非法请求

状态码实现见pastebin

于 2013-03-15T14:45:53.827 回答
2

根据我们之前的评论,只需进行一些更改即可让您重新站起来。

这些是,更改 v3 地理编码的地址

define("MAPS_HOST", "maps.googleapis.com");
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?";
$request_url = $base_url . "address=" . urlencode($address) . "&sensor=false";

其次更改返回的 xml 文件中的路径以设置 lat/long

$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];

可以完全替换为

$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;

下一篇关于阻止它超过地理编码限制的文章。您需要做的是在运行地理编码之前设置一个简单的检查。

while ($row = @mysql_fetch_assoc($result)) {
    if (!($row['lat'] * 1)) {// add this line
        $geocode_pending = true;
            while ($geocode_pending){
                //do geocoding stuff
            }
        }
    }// add this close
}
于 2013-03-17T21:32:25.583 回答