下面是我用来解决这个问题的案例陈述。它工作,但不知道它是否是最优雅的解决方案。
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()以删除我添加的空白区域,如果没有街道号。
我们在谈论什么样的编程语言? –
VB.net是我正在使用的。我想我可能不得不使用一个选择案例,并检查每个组件的类型。 – Mitchell
我认为,当涉及到你自己产生的数据时,你绝对不应该进行任何推定。在使用变量之前,确保变量存在。 –