直到最近,我一直在使用谷歌地理编码来提供要保存在数据库中的坐标。但是,当我最近尝试这样做时,它失败了,出现错误 610。我仍在使用 v2,我知道这正在逐步淘汰。所以,我来到这个网站并查看了这个帖子:从谷歌地图 api v2 到 v3 改变。我根据该线程的以下内容(我从反馈中了解到)更新了我的代码:
这些是,更改 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;
但仍然不适合我。当我尝试运行它时,我不再收到错误 610,只是“地理编码失败,错误代码”。
抱歉,我对这些东西还是比较陌生,我真的在学习,所以我很感激你能提供的任何帮助。这可能是我错过的最简单的事情。这是我目前的代码:
<?php
require("phpsqlgeocode_dbinfo.php");
define("MAPS_HOST", "maps.googleapis.com");
define("KEY", *my key*);
// Opens a connection to a MySQL server
$connection = mysql_connect('*name*', $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 all the rows in the markers table
$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/xml?";
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = $row["address"];
$id = $row["id"];
$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;
$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;
$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);
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 "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "
\n";
}
usleep($delay);
}
}
?>
非常感谢!