Google maps Excel VBA地理编码响应无效
我使用此脚本获取德国任何地址的地理代码:Google maps Excel VBA地理编码响应无效,google-maps,vba,geocoding,Google Maps,Vba,Geocoding,我使用此脚本获取德国任何地址的地理代码: Public Function getGoogleMapsGeocode(sAddr As String) As String Dim xhrRequest As XMLHTTP60 Dim sQuery As String Dim domResponse As DOMDocument60 Dim ixnStatus As IXMLDOMNode Dim ixnLat As IXMLDOMNode Dim ixnLng As IXMLDOMNode
Public Function getGoogleMapsGeocode(sAddr As String) As String
Dim xhrRequest As XMLHTTP60
Dim sQuery As String
Dim domResponse As DOMDocument60
Dim ixnStatus As IXMLDOMNode
Dim ixnLat As IXMLDOMNode
Dim ixnLng As IXMLDOMNode
' Use the empty string to indicate failure
getGoogleMapsGeocode = ""
Set xhrRequest = New XMLHTTP60
sQuery = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
sQuery = sQuery & Replace(sAddr, " ", "+")
xhrRequest.Open "GET", sQuery, True
xhrRequest.send
Set domResponse = New DOMDocument60
domResponse.LoadXML xhrRequest.responseText
Set ixnStatus = domResponse.SelectSingleNode("//status")
If (ixnStatus.Text <> "OK") Then
'Exit Function
End If
Set ixnLat = domResponse.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat")
Set ixnLng = domResponse.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng")
getGoogleMapsGeocode = ixnLat.Text & ", " & ixnLng.Text
End Function
当我在浏览器中键入查询字符串时,它将返回一个带有地理代码的有效xml响应。如果我让vba来做,我会得到一个错误。有什么我应该知道的吗
答复正文:
"<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>INVALID_REQUEST</status>
</GeocodeResponse>
" : String : Modul8.getGoogleMapsGeocode
它对我来说没有错误。函数或请求只是在浏览器中?我将你的函数粘贴到一个标准模块中,添加了对Microsoft XML 6.0的引用,当我输入地址时,我得到了坐标。它没有在responsetext上出错,因为它尚未完成加载。好吧,我撒了谎,我只犯了一个错误。但我进行了调试,然后F5继续运行,到那时xml已经加载。我没有收到无效的请求。