【问题标题】:Lookup City and State by Zip Google Geocode Api [closed]通过 Zip Google Geocode Api 查找城市和州 [关闭]
【发布时间】:2011-06-12 14:11:48
【问题描述】:

我基本上想检索邮政编码中的城市和州列表。 Google 的 Geocode API 是否能够做到这一点?我试过查看文档,但发现信息太多了。

任何帮助将不胜感激。如果有其他方法可以完成这样的任务,请告诉我。

谢谢

编辑:我能够通过 http://maps.google.com/maps/geo?output=xml&q=14606 检索城市和州,但有限制吗?

【问题讨论】:

  • 您可以使用HERE Maps Geocoding REST APICreate a project 并获取 REST 凭据。等待 1 小时让他们上线(首先向我显示 InvalidCredentials 错误),然后调用例如geocoder.cit.api.here.com/6.2/geocode.json?PostalCode=2400&country=Denmark&app_id={APP_ID}&app_code={APP_CODE}&gen=9(可以提供the address parts

标签: google-api google-geocoder


【解决方案1】:

【讨论】:

  • 你的意思是sensor=false
  • "注意:Geocoding API 只能与 Google 地图一起使用;禁止地理编码结果而不显示在地图上。"
  • @Rup 传感器不再需要
  • 这是谷歌搜索“城市邮政编码 api”时在谷歌上的第一次点击,这显然不是 :(
  • @KevinLeary result_type 不限于邮政编码。 Google 的地理编码器会将邮政编码误认为是街道地址号码(尝试 zip 89100)。相反,使用的格式是maps.googleapis.com/maps/api/geocode/…,如下所述:developers.google.com/maps/documentation/geocoding/…
【解决方案2】:

我发现了几种使用基于 Web 的 API 的方法。我认为US Postal Service 是最准确的,因为邮政编码是他们的东西,但Ziptastic 看起来更容易。

使用美国邮政服务 HTTP/XML API

根据page on the US Postal Service website which documents their XML based web API,特别是this PDF document 的第 4.0 节(第 22 页),他们有一个 URL,您可以在其中发送包含 5 位邮政编码的 XML 请求,他们将响应包含相应的 XML 文档城市和州。

根据他们的文档,您将发送以下内容:

http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>

以下是您将收到的回复:

<?xml version="1.0"?> 
<CityStateLookupResponse> 
    <ZipCode ID="0"> 
        <Zip5>90210</Zip5> 
        <City>BEVERLY HILLS</City> 
        <State>CA</State> 
    </ZipCode> 
</CityStateLookupResponse>

USPS确实要求您在使用 API 之前向他们注册,但据我所知,访问是免费的。顺便说一句,他们的 API 还有一些其他功能:您可以进行地址标准化和邮政编码查找,以及一整套跟踪、运输、标签等。

使用 Ziptastic HTTP/JSON API (no longer supported)

更新:自 2017 年 8 月 13 日起,Ziptastic 现已成为付费 API,可在 here

找到

这是一项相当新的服务,但根据他们的文档,看起来您需要做的就是向http://ziptasticapi.com 发送一个 GET 请求,如下所示:

GET http://ziptasticapi.com/48867

他们将返回一个 JSON 对象,如下所示:

{"country": "US", "state": "MI", "city": "OWOSSO"}

确实,它有效。您可以通过执行以下操作从命令行进行测试:

curl http://ziptasticapi.com/48867 

【讨论】:

  • 非常喜欢 Ziptastic 推荐。
  • US Postal Service 可能会拒绝您的访问请求,除非您将他们的 API 用于与邮寄相关的活动。至少,这是我的经验。
  • @eaj 没错:“地址验证 API 只能与 USPS 运输或邮寄服务结合使用。”
  • Ziptastic is no longer supported. 不应该再使用了。
  • @RationalRabbit:我提交了一个更新,指出 Ziptastic 现在是一个付费 API。
【解决方案3】:
function getCityState($zip, $blnUSA = true) {
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $zip . "&sensor=true";

    $address_info = file_get_contents($url);
    $json = json_decode($address_info);
    $city = "";
    $state = "";
    $country = "";
    if (count($json->results) > 0) {
        //break up the components
        $arrComponents = $json->results[0]->address_components;

        foreach($arrComponents as $index=>$component) {
            $type = $component->types[0];

            if ($city == "" && ($type == "sublocality_level_1" || $type == "locality") ) {
                $city = trim($component->short_name);
            }
            if ($state == "" && $type=="administrative_area_level_1") {
                $state = trim($component->short_name);
            }
            if ($country == "" && $type=="country") {
                $country = trim($component->short_name);

                if ($blnUSA && $country!="US") {
                    $city = "";
                    $state = "";
                    break;
                }
            }
            if ($city != "" && $state != "" && $country != "") {
                //we're done
                break;
            }
        }
    }
    $arrReturn = array("city"=>$city, "state"=>$state, "country"=>$country);

    die(json_encode($arrReturn));
}

【讨论】:

  • 你的部分代码帮助了我一点,非常感谢:)
【解决方案4】:

几个月前,我的一个项目也有同样的要求。我搜索了一下,发现了以下解决方案。这不是唯一解决方案,但我发现它是更简单之一。

使用http://www.webservicex.net/uszip.asmx 的网络服务。
具体GetInfoByZIP()方法。

您将能够通过任何邮政编码 (ex: 40220) 进行查询,您将收到如下回复...

<?xml version="1.0" encoding="UTF-8"?>
 <NewDataSet>
  <Table>
   <CITY>Louisville</CITY> 
   <STATE>KY</STATE> 
   <ZIP>40220</ZIP> 
   <AREA_CODE>502</AREA_CODE> 
   <TIME_ZONE>E</TIME_ZONE> 
  </Table> 
</NewDataSet>

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2014-10-18
    • 2017-10-27
    • 1970-01-01
    • 2015-08-01
    • 2018-07-25
    • 1970-01-01
    • 2017-09-02
    相关资源
    最近更新 更多