1

我在使用 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);
  }
}

?>
4

2 回答 2

3

如果您尝试使用写于 2007 年 10 月的 Pamela Fox 和 Tom Manshreck 文章Geocoding Addresses with PHP MySQL,请了解它是为 Geocode v2 编写的,不适用于 v3。这是文章中的代码,以及您需要对 Geocode v3 进行的更改。

<?php
require("phpsqlajax_dbinfo.php");

// THE HOST HAS CHANGED, AND A KEY IS NO LONGER REQUIRED.
// v2 define("MAPS_HOST", "maps.google.com");
// v2 define("KEY", "abcdefg");
define("MAPS_HOST", "maps.googleapis.com"); // V3

// 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 all the rows in the markers table
$query = "SELECT * FROM markers2 WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;

// THE BASE URL HAS CHANGED
// v2 $base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?"; // V3

// 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"];

    // THE REQUEST URL HAS CHANGED
    // v2 $request_url = $base_url . "&q=" . urlencode($address);
    $request_url = $base_url . "address=" . urlencode($address) . "&sensor=false"; // v3

    $xml = simplexml_load_file($request_url) or die("url not loading");

    // THE STATUS CHECK HAS CHANGED
    // v2 $status = $xml->Response->Status->code;
    // v2 if (strcmp($status, "200") == 0) {
    $status = $xml->status; // v3
    if (strcmp($status, "OK") == 0) { // v3

      // successful geocode
      $geocode_pending = false;
     // $coordinates = $xml->Response->Placemark->Point->coordinates;          

      // THE COORDINATS SPLIT HAS CHANGED
      // v2 $coordinatesSplit = split(",", $coordinates);
      // v2 $lat = $coordinatesSplit[0];
      // v2 $lng = $coordinatesSplit[1];

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

      $query = sprintf("UPDATE markers2 " .
             " 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);
  }
}
?>

希望这对大家有帮助。

于 2013-07-23T00:00:38.717 回答
2

干得好,你需要注释掉 $coordinates,并且 location->Lat & Lng 不应该大写。结果->几何->位置->纬度和结果->几何->位置->lng

于 2013-09-18T18:05:59.337 回答