引用此https://developers.google.com/maps/documentation/places/#PlaceDetails
我希望JSON结果始终包含所有address_components
个详细信息。但是,我的一次搜索最终丢失了street_number,postal_code和其中一个。在intellisense中查看,其中一个address_components
返回了一个administrative_area_level_2。
这让我陷入了一个循环,因为我总是希望返回那些原始值,即使它们可能是空的。我也像这样硬编码预期的回报:
array.result.address_components(0).long_name
由于我不能指望组件返回所有结果,我需要一种新的方法来适当地访问它们并找出当我错过一些时要做什么。
答案 0 :(得分:0)
这是我用来处理这个问题的案例陈述。它有效,但不确定它是否是最优雅的解决方案。
Dim street As String = ""
Dim city As String = ""
Dim state As String = ""
Dim country As String = ""
Dim zip As String = ""
Dim phone As String = ""
Dim website As String = ""
' No guareentee to the order of even if the data will be there so we check each type. '
For j = 0 To array.result.address_components.Length - 1
Select Case array.result.address_components(j).types(0)
Case "street_number"
street += array.result.address_components(j).long_name()
Case "route"
street += " " & array.result.address_components(j).long_name()
Case "locality"
city = array.result.address_components(j).long_name()
Case "administrative_area_level_1"
state = array.result.address_components(j).long_name()
Case "country"
country = array.result.address_components(j).long_name()
Case "postal_code"
zip = array.result.address_components(j).long_name()
End Select
Next
我基本上循环遍历address_components并检查其类型值。如果值是一个我正在寻找我分配它。我后来在街道上调用Trim()来删除我添加的空白区域,如果没有街道号码的话。