Я изо всех сил пытаюсь получить полное имя адреса с помощью кода VBA, который я сейчас использую,
Я думаю, что моя проблема заключается в разделе street_number, но когда я изменяю его на long_name, результаты не генерируются.
Я новичок в VBA, любая помощь в этом вопросе будет оценена.Спасибо
Sub myTest()
Dim xhrRequest As XMLHTTP60
Dim domDoc As DOMDocument60
Dim domDoc2 As DOMDocument60
Dim placeID As String
Dim query As String
Dim nodes As IXMLDOMNodeList
Dim node As IXMLDOMNode
Dim rng As Range, cell As Range
Set rng = Range("B1")
For Each cell In rng
'you have to replace spaces with +
query = cell.Value
'You must acquire a google api key and enter it here
Dim googleKey As String
googleKey = "Your API key" '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?" & _
"query=" & query & "&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
Dim output As String
Dim s As String
'hacky way to get postal code, you might want to rewrite this after learning more
Set nodes = domDoc.SelectNodes("//result/address_component/type")
For Each node In nodes
s = node.Text
If s = "street_number" Then
'this is bad, you should search for "long_name", what i did here was assume that "long_name was the first child"
'output = vbNewLine & "Postal Code: " & node.ParentNode.FirstChild.Text
cell.Offset(0, 1).Value = "Address: " & node.ParentNode.FirstChild.Text
End If
If s = "postal_code" Then
'this is bad, you should search for "long_name", what i did here was assume that "long_name was the first child"
'output = vbNewLine & "Postal Code: " & node.ParentNode.FirstChild.Text
cell.Offset(0, 2).Value = "Postal Code: " & node.ParentNode.FirstChild.Text
End If
Next node
Next cell
'output
'MsgBox "Formatted Address: " & domDoc.SelectSingleNode("//result/formatted_address").Text & output
End Sub
Что я получаю обратно:
Что мне нужно: