使用Google Places API时无结果

时间:2019-02-18 15:55:33

标签: excel vba api google-places-api

我的项目是完成通讯录中已经存在的数据(大约500个条目,其中大约100个丢失)。

使用此处(How to retrieve address full name by using VBA and Google API key?)中的代码,我对其进行了调整,以使用Google Places API进行所需的搜索。

显然,VBA明智的代码是正确的,但运行它时我将得到错误400或根本没有数据。

这是我的代码:

Sub GetAddress()
    Dim xhrRequest As XMLHTTP60
    Dim domDoc As DOMDocument60
    Dim domDoc2 As DOMDocument60
    Dim placeID As String
    Dim pdv As String
    Dim nodes As IXMLDOMNodeList
    Dim node As IXMLDOMNode
    Dim xRow As Long


    Do While Cells(xRow, 1) <> ""

    'only look for missing address
    xRow = 1

    If Cells(xRow, 2) <> "" Then GoTo NextRow

    pdv = Cells(xRow, 1)

    'You must acquire a google api key and enter it here
    Dim googleKey As String
    googleKey = "MY API" 'your api key here

    'Send a "GET" request for place/textsearch
    Set xhrRequest = New XMLHTTP60

    xhrRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/textsearch/xml?" & _
       "pdv &key=" & googleKey, False
    xhrRequest.send

    'Save the response into a document
    Set domDoc = New DOMDocument60
    domDoc.LoadXML xhrRequest.responseText

    'Find the first node that is called "place_id" and is the child of the "result" node
    placeID = domDoc.SelectSingleNode("//result/place_id").Text

    'recycling objects (could just use new ones)
    Set domDoc = Nothing
    Set xhrRequest = Nothing

    'Send a "GET" request for place/details
    Set xhrRequest = New XMLHTTP60
    xhrRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/details/xml?placeid=" & placeID & _
    "&key=" & googleKey, False
    xhrRequest.send

    'Save the response into a document
    Set domDoc = New DOMDocument60
    domDoc.LoadXML xhrRequest.responseText

    'output
      Cells(xRow, 2).Value = domDoc.SelectSingleNode("//result/formatted_address").Text & output

NextRow:
    xRow = xRow + 1
    Loop
End Sub

我不明白为什么它不返回任何结果,我是VBA的新手。

0 个答案:

没有答案