我对这个有点困惑。
我正在尝试查找 lats & lngs 的商店定位器,并且一直在使用 Google 的 Geolocator 及其在http://code.google.com/apis/maps/articles/phpsqlgeocode.html上的教程,但似乎无法返回除可怕的 620 Too many queries 错误之外的任何内容。
对我来说真正奇怪的是,如果我将 $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);
}
}
?>