Google Geocoder缺少类型地址组件

时间:2016-11-14 23:10:18

标签: google-maps google-maps-api-3 geolocation geocoding

目前我对谷歌地理编码器感到难过。现在,如果我按地址查询“纽约市纽约市”,我会得到结果,说结果的类型是“类型”:[“locality”,“political”]但是address_components中没有地点。如果我查询“坦佩,亚利桑那州”,同样的事情。如果我搜索某个州,则类型为administrative_area_level_1,但它只给我address_compenents中的国家/地区。有什么想法吗?

var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': "New York City, NY"}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
         //do stuff
    } else {
        console.log("Geocode was not successful for the following reason: " + status);
    }

“纽约州纽约市”的结果

[{"address_components":[{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"New York, USA","geometry":{"bounds":{"south":40.4773991,"west":-74.25908989999999,"north":40.9175771,"east":-73.7002721},"location":{"lat":40.7127837,"lng":-74.00594130000002},"location_type":"APPROXIMATE","viewport":{"south":40.4960439,"west":-74.2557349,"north":40.9152555,"east":-73.7002721}},"place_id":"ChIJOwg_06VPwokRYv534QaPC8g","types":["locality","political"]}]

我很困惑,为什么它告诉我它是类型城市(地方),但缺少locality属性。我希望地方在那里有值,“纽约”和格式化的地址,以包括其中的位置。谢谢!

1 个答案:

答案 0 :(得分:5)

永远不要假设地址组件中始终存在"locality"或任何其他类型。

请参阅gmaps-api-issues #3320以及Place Autocomplete Address Form示例中的较长解释。

如果你真的需要一个特定的组件,reverse geocoding是获得它的唯一可靠方法(也就是说,它实际存在的地方)。