Google api 通过Zip谷歌地理编码Api查找城市和州

Google api 通过Zip谷歌地理编码Api查找城市和州,google-api,google-geocoder,Google Api,Google Geocoder,我基本上想要在zipcode中检索城市和州的列表。谷歌的地理编码API能够做到这一点吗?我试着查看文档,但发现信息铺天盖地 任何帮助都将不胜感激。如果有其他方法来完成这项任务,请告诉我 谢谢 编辑:我可以通过:检索城市和州,但是有限制吗?使用 例如,要查找zip 77379,请使用如下请求: 我找到了几种使用基于web的API实现这一点的方法。我认为这将是最准确的,因为邮政编码是他们的东西,但看起来容易得多 使用美国邮政服务HTTP/xmlapi 根据这一点,特别是第4.0节(第22页),他们有

我基本上想要在zipcode中检索城市和州的列表。谷歌的地理编码API能够做到这一点吗?我试着查看文档,但发现信息铺天盖地

任何帮助都将不胜感激。如果有其他方法来完成这项任务,请告诉我

谢谢

编辑:我可以通过:检索城市和州,但是有限制吗?

使用

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


我找到了几种使用基于web的API实现这一点的方法。我认为这将是最准确的,因为邮政编码是他们的东西,但看起来容易得多

使用美国邮政服务HTTP/xmlapi 根据这一点,特别是第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>
它们将返回一个JSON对象,如下所示:

{"country": "US", "state": "MI", "city": "OWOSSO"}
事实上,它是有效的。您可以通过执行以下操作从命令行进行测试:

curl http://ziptasticapi.com/48867 

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

使用位于的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>

路易斯维尔
基尼
40220
502
E

希望这有帮助……

你的意思是?“注意:地理编码API只能与谷歌地图一起使用;禁止在地图上不显示地理编码结果。”@Rup传感器不再是必需的这是谷歌搜索“城市邮政编码API”时第一次点击谷歌,这显然不是:(@KevinLeary result_type不限于邮政编码。谷歌的地理编码器会将邮政编码误认为是街道地址号码(试试邮政编码89100)。相反,使用的格式如下所述:Major love for the Ziptastic Recommension。美国邮政服务可能会拒绝您的访问请求,除非您将其API用于与邮件相关的活动。至少,这是我的经验。@eaj这是真的:“地址验证API只能与USPS SHIPPING或Mail SERVICES结合使用。”不应再使用它。@RationalRabbit:我提交了一个更新,指出Ziptasic现在是付费API。您可以使用。并获取其余凭据。等待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
(您可以提供)您的部分代码对我有点帮助,非常感谢:)
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));
}
<?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>
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));
}