因此,我正要在 VBA 中完成我的地理编码脚本,但在 beta 测试期间,我注意到它无法提取大多数地址的数据,而对于其他地址,它工作得很好。代码如下:
Sub newReadXMLData(link As String)
Dim odc As DOMDocument
Dim lat As String
Dim lng As String
Dim location As IXMLDOMElement
Dim locationPath As String
Dim i As Integer
Set odc = New MSXML2.DOMDocument
odc.async = False
odc.Load (link)
locationPath = "GeocodeResponse/result/geometry[location_type='ROOFTOP']/location"
Set location = odc.SelectSingleNode(locationPath)
lat = GetTextValue(location, "./lat")
lng = GetTextValue(location, "./lng")
Debug.Print lat & "; " & lng
End Sub
Function GetTextValue(node As IXMLDOMElement, Optional xpath As String = "") As String
Dim selectedNode As IXMLDOMElement
If xpath <> "" And Not node Is Nothing Then
Set selectedNode = node.SelectSingleNode(xpath)
Else
Set selectedNode = node
End If
If selectedNode Is Nothing Then
GetTextValue = ""
Else
GetTextValue = Trim(selectedNode.Text)
End If
End Function
该脚本返回正确的值,例如 forhttp://maps.googleapis.com/maps/api/geocode/xml?address=1+Infinite+Loop,+Cupertino,+Santa+Clara,+California+95014&sensor=false
但它不返回任何内容,例如http://maps.googleapis.com/maps/api/geocode/xml?address=BAGIENNA+13,+88-100+INOWROCŁAW&sensor=false
。谁能解释这是为什么?