目前,我对Google Geocoder感到困惑。现在,如果我按地址“ New York City,NY”查询,则会返回结果,结果的类型为“ types”:[[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"]}]


我很困惑,为什么它告诉我它是type city(locality),但缺少locality属性。我希望该位置带有“ New York”值,格式地址也包含该位置。谢谢!

最佳答案

永远不要假设"locality"或任何其他类型总是出现在地址组件中。

请参见gmaps-api-issues #3320Place Autocomplete Address Form示例中的更详细说明。

如果确实需要特定的组件,则reverse geocoding是始终获取它的唯一可靠方法(即在实际存在的地方)。

10-06 09:28