通过Zip Google Geocode Api查找城市和州

mee*_*nxo 75 google-api google-geocoder

我基本上想要在邮政编码中检索城市和州的列表.Google的Geocode API是否有能力这样做?我已经尝试查看文档,但发现信息压倒性.

任何帮助,将不胜感激.如果还有其他方法可以完成此类任务,请告诉我.

谢谢

编辑:我能够通过以下网址检索城市和州:http://maps.google.com/maps/geooutput = xml&q = 14606但是对此有限制吗?

Jas*_*son 105

使用GeoCoding API

例如,要查找zip 77379,请使用如下请求:

http://maps.googleapis.com/maps/api/geocode/json?address=77379&sensor=true

  • "注意:地理编码API只能与Google地图一起使用;禁止在地图上显示地理编码结果,而不会在地图上显示地理编码结果." (17认同)
  • 你的意思是[sensor = false](http://code.google.com/apis/maps/documentation/staticmaps/#Sensor)? (9认同)
  • @KevinLeary result_type不限制邮政编码.Google的地理编码器会将邮政编码误认为是街道地址编号(请尝试使用zip 89100).相反,使用的格式为http://maps.googleapis.com/maps/api/geocode/json?components=country:US |postal_code:00001,如下所示:https://developers.google.com/maps/documentation /地理编码/前奏#ComponentFiltering (6认同)
  • 不再需要@Rup传感器 (2认同)
  • 这是谷歌搜索“城市邮编api”时在Google上的第一招,而且显然不是:( (2认同)

Ric*_*nes 49

我发现了几种基于Web的API的方法.我认为美国邮政服务将是最准确的,因为邮政编码是他们的东西,但Ziptastic看起来更容易.

使用US Postal Service HTTP/XML API

根据美国邮政服务网站上的这个页面记录了他们基于XML的Web API,特别是这个PDF文档的第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>
Run Code Online (Sandbox Code Playgroud)

这是你会得到的回报:

<?xml version="1.0"?> 
<CityStateLookupResponse> 
    <ZipCode ID="0"> 
        <Zip5>90210</Zip5> 
        <City>BEVERLY HILLS</City> 
        <State>CA</State> 
    </ZipCode> 
</CityStateLookupResponse>
Run Code Online (Sandbox Code Playgroud)

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

使用Ziptastic HTTP/JSON API(不再支持)

更新:截至2017年8月13日,Ziptastic现已成为付费API,可在此处找到

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

GET http://ziptasticapi.com/48867
Run Code Online (Sandbox Code Playgroud)

他们将返回一个JSON对象:

{"country": "US", "state": "MI", "city": "OWOSSO"}
Run Code Online (Sandbox Code Playgroud)

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

curl http://ziptasticapi.com/48867 
Run Code Online (Sandbox Code Playgroud)

  • 对Ziptastic推荐的热爱.<3! (7认同)
  • US Postal Service可能会拒绝您的访问请求,除非您将其API用于与邮件相关的活动.至少,那是我的经历. (5认同)
  • [Ziptastic不再受支持.](https://github.com/joshstrange/Ziptastic)不应该再使用它了. (2认同)
  • @RationalRabbit:我提交了一份更新,指出Ziptastic现在是付费API. (2认同)

Nic*_*enc 14

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));
}
Run Code Online (Sandbox Code Playgroud)


Som*_*dar 6

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

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

您将能够通过任何zipcode(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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...

  • 网址现在不能用了! (2认同)