因此,我正要在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该脚本返回正确的值,例如http://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。谁能解释一下这是为什么?
发布于 2012-11-17 17:43:18
我想可能是编码错误。我让您的两个字符串在浏览器中都能正常工作,但是当我尝试您的VBA代码时,第二个字符串返回ZERO_RESULTS。INOWROCŁAW中有一个字符在VBA中似乎不起作用(我在Word 2010中尝试过)。
发布于 2012-11-17 00:16:06
似乎Olle Sjögren是对的--这个问题是由地址中的特殊字符引起的。一个简单的字符转换函数就可以完成这项工作。
https://stackoverflow.com/questions/13417285
复制相似问题