【问题标题】:Google Maps geocode api doesnt return country in responseGoogle Maps geocode api 不返回国家/地区作为响应
【发布时间】:2016-12-08 08:32:48
【问题描述】:

我正在使用 google Geocode api 从我发送的自定义语言环境中获取坐标和国家/地区。

https://maps.googleapis.com/maps/api/geocode/json?address=13+Vernon+Park+Singapore+367835&key=############################

我从中得到的回应是

{
 "results" : [
  {
     "address_components" : [
        {
           "long_name" : "13",
           "short_name" : "13",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Vernon Park",
           "short_name" : "Vernon Park",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Toa Payoh",
           "short_name" : "Toa Payoh",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "367835",
           "short_name" : "367835",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "13 Vernon Park, Singapore 367835",
     "geometry" : {
        "location" : {
           "lat" : 1.340062,
           "lng" : 103.880577
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 1.341410980291502,
              "lng" : 103.8819259802915
           },
           "southwest" : {
              "lat" : 1.338713019708498,
              "lng" : 103.8792280197085
           }
        }
     },
     "place_id" : "ChIJbXRmsZAX2jERv_hlVeqBXtM",
     "types" : [ "street_address" ]
  }
 ],
 "status" : "OK"
}

我为从响应中获取国家/地区而编写的 php 代码是,

foreach($response['results'][0]['address_components'] as $addressComponet){
    if(in_array('country', $addressComponet['types'])) {
        if($addressComponet['short_name'] != $addressComponet['long_name'])
            $country = $addressComponet['long_name'];
        else
            $country = $addressComponet['long_name'];
    }
}

$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lng'];
$latitude = $geometry['location']['lat'];

$array = array(
    'latitude' => $latitude,
    'longitude' => $longitude,
    'location_type' => $geometry['location_type'],
    'country' => $country
);

其中$response 包含 curl_exec 响应。直到昨天,这似乎一直运行良好。但是,从今天开始,我没有在数组中获取国家/地区字符串。 api 响应有什么我不知道的变化吗?

【问题讨论】:

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


【解决方案1】:

我认为响应已更改,因此我继续在 Google Maps API 问题部分提交了一个错误。

https://code.google.com/p/gmaps-api-issues/issues/detail?id=11024

原来他们在地理编码 API 中添加了一个新的正向地理编码器。他们还添加了一个标志,将请求重定向到该 API 中的旧正向地理编码器。

现在的重点是,既然他们升级了地理编码 API,我们应该如何处理数据库中存在的此类语言环境?我们已经在使用谷歌地图自动完成功能来防止随机无用的错误语言环境通过 API。

【讨论】:

    【解决方案2】:

    通常,地理编码 API 返回与相关国家/地区的地址格式相关的地址组件。

    新加坡的典型地址是:

    Block 123 Marina Avenue 2 #15-222 Singapore 123456

    6 Ayer Rajah Crescent Singapore 139962

    它们不包含国家/地区名称,因此 Geocoding API 不会返回此地址组件,因为它与地址格式无关。

    更新

    如果您需要检查特定语言环境的国家/地区,可行的解决方法是将结果类型设置为国家/地区的语言环境坐标的反向地理编码。

    例如

    搜索'6 Ayer Rajah Crescent Singapore 139962'

    https://maps.googleapis.com/maps/api/geocode/json?address=6%20Ayer%20Rajah%20Crescent%20Singapore%20139962&key=YOUR_API_KEY

    您将获得坐标 1.294947,103.787603。现在执行反向地理编码,结果类型等于国家/地区。

    https://maps.googleapis.com/maps/api/geocode/json?latlng=1.294947%2C103.787603&result_type=country&key=YOUR_API_KEY

    响应将是

    {
        "results":[
    {
      "address_components":[
        {
          "long_name":"Singapore",
          "short_name":"SG",
          "types":[
            "country","political"
          ]
        }
      ],
      "formatted_address":"Singapore",
      "geometry":{
        "bounds":{
          "northeast":{
            "lat":1.4784001,"lng":104.0945001
          },
          "southwest":{
            "lat":1.1496,"lng":103.594
          }
        },
        "location":{
          "lat":1.352083,"lng":103.819836
        },
        "location_type":"APPROXIMATE",
        "viewport":{
          "northeast":{
            "lat":1.4707592,"lng":104.0884808
          },
          "southwest":{
            "lat":1.1587023,"lng":103.6055575
          }
        }
      },
      "place_id":"ChIJdZOLiiMR2jERxPWrUs9peIg",
      "types":[
        "country","political"
      ]
    }
      ],
      "status":"OK"
    }
    

    【讨论】:

    • 是的,但是我们应该如何知道特定语言环境是否属于该国家/地区?另外,为什么 Vermon Park 地区在前一天返回该国作为回应?
    • 这是我想到的另一种解决方案。感谢分享@xomena 但是,我们还有其他解决方案可以减少此 api 调用吗?
    • Google 似乎接受了issue 11070,这应该可以解决您的问题。希望实施会很快。
    • 感谢@xomena。这很有帮助。感谢您发现它。
    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 2012-07-29
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多