我在使用 PHP 页面和 Google 的地理编码服务时遇到问题。我是新手,可能会忽略一些简单的事情。我从 Google 地理团队于 2007 年编写的 PHP 代码开始,我相信我的问题与将此 Geocode V2 代码改编为当前 V3 代码有关。以下是三种改编:
// Change the maps host, and keys are no longer used:
//define("MAPS_HOST", "maps.google.com"); V2 Geocode
//define("KEY", "abcdefg"); V2 Geocode
define("MAPS_HOST", "maps.googleapis.com"); // V3 Geocode
// Change the base URL:
//$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY V2 Geocode
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?"; //V3 Geocode
// Change the request URL:
// $request_url = $base_url . "&q=" . urlencode($address); V2 Geocode
$request_url = $base_url . "address=" . urlencode($address) . "&sensor=false"; V3 Geocode
我在 MySQL 数据库中使用四个实际地址进行测试;这些地址通常出现在 maps.google.com 中。使用下面的 PHP,所有四个地址都会抛出此错误:“此地址 1744 Crescent Ave, Idaho Falls, Idaho 83402 未能再次进行地理编码。Received status' (当然有四个不同的地址和四个错误。)没有收到状态码。我通过 echo 检查了 $Address 变量,它似乎是正常形成的。
提前致谢....
这是整个 PHP 页面:
<?php
require("../../DatabaseCredentials.php");
define("MAPS_HOST", "maps.googleapis.com");
// 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());
}
// Select certain fields in the Houses table
$query = "
SELECT `Active`, `SaleTypeID`, `HouseID`, `Active`, `StreetNumber`, `StreetCardinal`, `StreetName`, `City`, `State`, `ZipCode`, `Latitude`, `Longitude`
FROM `Houses`
WHERE `HouseID` = 1519 OR `HouseID` = 1520 OR `HouseID` = 1525 OR `HouseID` = 1526
";
$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/xml?";
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = $row["StreetNumber"]." ".$row["StreetCardinal"]." ".$row["StreetName"].", ".$row["City"].", ".$row["State"]." ".$row["ZipCode"];
$id = $row["HouseID"];
$request_url = $base_url . "address=" . urlencode($address) . "&sensor=false";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
$Latitude = $coordinatesSplit[0];
$Longitude = $coordinatesSplit[1];
$query = sprintf("UPDATE brents8_houses " .
" 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;
} else {
// failure to geocode
$geocode_pending = false;
echo "This Address " . $address . " failed to geocoded again. ";
echo "Received status " . $status . "
\n";
}
usleep($delay);
}
}
?>