Google Reverse-Geocode - 获取邮政编码

时间:2011-01-14 03:23:02

标签: php google-maps google-maps-api-3 google-maps-api-2

这是关于我如何获得不同组件的当前代码。

function getGeocodeFull($address)
{
    if (empty($address)) return false;

    $_geocode = false; 

    if (($_geocode = $this->getCache($address)) === false) // check if address is has a cache
    {
        if (($results = $this->geoGetCoordsFull($address)) !== false) 
        {
            // sloooooooooopppy.. yes these can all be combined, i know.
            if (!isset($results->results[0]->geometry->location->lat))  return false;
            if (!isset($results->results[0]->geometry->location->lat))  return false;
            if (!isset($results->results[0]->formatted_address))        return false;
            if (!isset($results->results[0]->address_components))       return false;

            $_geocode['lat']     = $results->results[0]->geometry->location->lat;
            $_geocode['lon']     = $results->results[0]->geometry->location->lng;
            $_geocode['address'] = $results->results[0]->formatted_address;

            foreach ($results->results[0]->address_components as $component)
            {
                if (isset($component->types))
                {
                    foreach ($component->types as $type)
                    {
                        switch ($type)
                        {
                            case 'route':
                                $_geocode['street'] = $component->long_name;
                                break;
                            case 'locality':
                                $_geocode['city'] = $component->long_name;
                                break;
                            case 'administrative_area_level_2':
                                $_geocode['county'] = $component->long_name;
                                break;
                            case 'administrative_area_level_3':
                                $_geocode['area'] = $component->long_name;
                                break;
                            case 'postal_code':
                                $_geocode['zip'] = $component->short_name;
                                break;
                        }     
                    }
                }    
            }

            $this->putCacheFull($address, $_geocode['lat'], $_geocode['lon'], $_geocode['address'], $_geocode['street'], $_geocode['city'], $_geocode['county'], $_geocode['area'], $_geocode['zip']);
        }
    }

    return $_geocode;
}

geoGetCoordsFull()只是在google map api地理编码页面上使用file_get_contents,然后解码结果然后返回它们。

我对这看起来并不太喜欢。看起来它可能非常不一致。有没有其他更好的解决方案来获取谷歌或任何其他服务的所有数据?

我更喜欢另一种不限制请求的服务(或者使用多种地理编码服务是非法的,例如bing / yahoo / etc)? (抱歉抛出另一个问题)

无论如何,非常感谢非常感谢!

1 个答案:

答案 0 :(得分:1)

我换了另一种方式,为了简洁,我删除了一些代码,但基本上我有一个大约16,000个位置的表(澳大利亚的所有位置)并通过它们循环以在Google地图上找到它们,检索lat长信息并存储回表中,这样就不必再查找了

$ row包含我正在查找的位置的详细信息数组,例如地址,邮政编码,郊区和州

您还需要从Google地图获取此密码的API密钥,因为它使用v2。我认为你不再需要v3了

define("MAPS_HOST", "maps.google.com");
define("KEY", "YOU_KEY_HERE");

// Initialize delay in geocode speed
$delay = 0;
$lat = '';
$lng = '';
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

  $geocode_pending = true;
  while ($geocode_pending) {

    flush();

    $address = $row['suburb'] . ' ' . $row['postcode'] . ', ' . $row['state'] . ' Australia';
    $request_url = $base_url . "&q=" . urlencode($address);
    $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);

      // Format: Longitude, Latitude, Altitude
      $lat = $coordinatesSplit[1];
      $lng = $coordinatesSplit[0];

    } else if (strcmp($status, "620") == 0) {
      // sent geocodes too fast, have a breather and try again soon
      echo("Google says no... increase the delay to ".($delay/1000000)." seconds<br />\n");
      $delay += 100000;
    } else {
      // failure to geocode
      $geocode_pending = false;
      echo "Address " . $address . " failed to be geocoded. ";
      echo "Received status " . $status . "<br />
\n";
    }
    usleep($delay);
  }
  echo("Lat long found for " . $address . "<br />\n");
  echo("Lat: " . $lat . "<br />\n");
  echo("Long: " . $lng . "<br />\n");