我对此有点困惑。
我正在尝试查找lats&lngs的商店定位器,并一直在使用谷歌的Geolocator和他们的教程http://code.google.com/apis/maps/articles/phpsqlgeocode.html,但除了可怕的620查询过多错误之外,似乎无法返回任何其他内容。
对我来说,真正奇怪的是,如果我将$request_url复制到浏览器中,我会获得预期的所有XML,但使用PHP的simplexml_load_file($request_url)总是返回错误代码620,无论延迟多长时间,无论我使用多少不同的API密钥。
有人知道这是怎么回事吗?我是不是犯了什么可怕的错误?
我的代码如下,请原谅广泛的评论和回应:
<?php
$host = 'xxx';
$db = 'xxx';
$u = 'xxx';
$p = 'xxx';
define("MAPS_HOST", "maps.google.com");
define("KEY", "xxx");
// Opens a connection to a MySQL server
$connection = mysql_connect($host, $u, $p);
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($db, $connection);
if (!$db_selected) {
die("Can''t use db : " . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM BH_locations";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
echo '<br /><br />row#' . $row['id'] . '<br />';
$geocode_pending = true;
while ($geocode_pending) {
echo 'started while loop at ' . date('G:i:s:u') . '<br />';
$address = $row["address"] . ', ' . $row['city'] . ', ' . $row['state'] . ' ' . $row['zip'];
echo $address . '<br />';
$id = $row["id"];
$request_url = $base_url . "&q=" . urlencode($address);
echo 'request url: ' . $request_url . '<br />';
$xml = simplexml_load_file($request_url) or die("url not loading");
echo '<pre>';
var_dump($xml);
echo '</pre>';
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
echo '<br /><strong>WOOHOO IT WORKED!!!</strong><br />';
$geocode_pending = false;
/*
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
$query = sprintf("UPDATE BH_locations " .
" SET latitude = '%s', longitude = '%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);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
*/
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
echo 'error 620<br /><br />';
} else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "'n";
}
usleep($delay);
}
}
?>
您发送的请求太多,很可能速度太快。
请参阅谷歌地图文档中的G_GEO_TOO_MANY_QUERIES常量。
给定的密钥在24小时内超过了请求限制或者在太短的时间内提交了太多请求。如果您并行或紧密循环发送多个请求,请在代码中使用计时器或暂停,以确保不会发送请求过快