如何将Google Places API(附近搜索)数据获取到Excel VBA

如何将Google Places API(附近搜索)数据获取到Excel VBA,vba,excel,web-scraping,google-places-api,Vba,Excel,Web Scraping,Google Places Api,我有很多地址(和匹配的坐标)的Excel表格。我想使用GooglePlacesAPI来创建一个函数,让我可以使用e。G所有餐厅周围都有一个特定的地址。我尝试输出所有匹配餐厅的名称,问题是输出仅为“0” 这是我的密码: 附近的功能(纬度尽可能长,液化天然气尽可能长)作为变型 “Variablen definieren Dim请求作为新的XMLHTTP30 Dim结果作为新的DOMDocument30 将StatusNode设置为IXMLDOMNode 将NearbyNode变暗为IXMLDOMNo

我有很多地址(和匹配的坐标)的Excel表格。我想使用GooglePlacesAPI来创建一个函数,让我可以使用e。G所有餐厅周围都有一个特定的地址。我尝试输出所有匹配餐厅的名称,问题是输出仅为“0”

这是我的密码:

附近的功能(纬度尽可能长,液化天然气尽可能长)作为变型
“Variablen definieren
Dim请求作为新的XMLHTTP30
Dim结果作为新的DOMDocument30
将StatusNode设置为IXMLDOMNode
将NearbyNode变暗为IXMLDOMNode
关于错误转到错误处理程序
请求。打开“获取”https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=“&Lat&”、“&Lng&”&radius=50&type=餐厅&key=AIzaSyA5nFPM\u 9Ss\u X410c35WfoP\u obp5wppru”“”
请求发送
Results.LoadXML Request.responseText
设置StatusNode=Results。选择SingleNode(//状态)
选择案例UCase(StatusNode.Text)
案例“OK”
设置NearbyNode=Results。选择singlenode(//result/name[1]/name[2]/name[3]/name[4]”)
nearnear=NearbyNode.Text
案例“零结果”
附近=“地址不存在”
其他情况
附近=“错误”
结束选择
错误处理程序:
设置StatusNode=Nothing
设置NearbyNode=Nothing
设置结果=无
设置请求=无
端函数

我稍微修改了您的代码,请尝试以下示例:

选项显式
子测试()
模糊的近义词
以字符串的形式暗显NearbyState
附近“-33.8670522”,“151.1957362”,“1000”,“医院”,NearbyNames,NearbyState
如果NearbyState=“确定”,则
MsgBox联接(近旁名称、vbCrLf)
其他的
MsgBox NearbyState
如果结束
端接头
附近的子系统(Lat为字符串、Lng为字符串、Dist为字符串、PointType为字符串、名称为变量、状态为字符串)
作为对象的Dim请求
将结果变暗为对象
暗节点
关于错误转到错误处理程序
Set Request=CreateObject(“MSXML2.XMLHTTP”)
请求。打开“获取”https://maps.googleapis.com/maps/api/place/nearbysearch/xml?" & _
“位置=”&Lat&“,”&Lng&_
“&radius=“&Dist&_
“&type=“&PointType&_
“&key=AIzaSyA5nFPM_9Ss_X410c35WfoP_obP5UwppRU”,错误
请求发送
设置结果=Request.responseXML
选择案例UCase(Results.SelectSingleNode(//status”).Text)
案例“OK”
使用CreateObject(“Scripting.Dictionary”)
对于Results.SelectNodes中的每个节点(“//PlaceSearchResponse/result/name”)
.Add.Count,Node.nodeTypedValue
下一个
名称=.Items()
以
State=“OK”
案例“零结果”
State=“无结果”
其他情况
State=“错误的请求”
结束选择
出口接头
错误处理程序:
State=“Error”
端接头
我的输出是:


我稍微修改了您的代码,请尝试以下示例:

选项显式
子测试()
模糊的近义词
以字符串的形式暗显NearbyState
附近“-33.8670522”,“151.1957362”,“1000”,“医院”,NearbyNames,NearbyState
如果NearbyState=“确定”,则
MsgBox联接(近旁名称、vbCrLf)
其他的
MsgBox NearbyState
如果结束
端接头
附近的子系统(Lat为字符串、Lng为字符串、Dist为字符串、PointType为字符串、名称为变量、状态为字符串)
作为对象的Dim请求
将结果变暗为对象
暗节点
关于错误转到错误处理程序
Set Request=CreateObject(“MSXML2.XMLHTTP”)
请求。打开“获取”https://maps.googleapis.com/maps/api/place/nearbysearch/xml?" & _
“位置=”&Lat&“,”&Lng&_
“&radius=“&Dist&_
“&type=“&PointType&_
“&key=AIzaSyA5nFPM_9Ss_X410c35WfoP_obP5UwppRU”,错误
请求发送
设置结果=Request.responseXML
选择案例UCase(Results.SelectSingleNode(//status”).Text)
案例“OK”
使用CreateObject(“Scripting.Dictionary”)
对于Results.SelectNodes中的每个节点(“//PlaceSearchResponse/result/name”)
.Add.Count,Node.nodeTypedValue
下一个
名称=.Items()
以
State=“OK”
案例“零结果”
State=“无结果”
其他情况
State=“错误的请求”
结束选择
出口接头
错误处理程序:
State=“Error”
端接头
我的输出是:


我将
Lat,Lng
更改为类型
double
,并修改了
Request.Open“GET”
字符串

Sub Nearby()

Dim Request                 As New XMLHTTP30
Dim Results                 As New DOMDocument30
Dim StatusNode              As IXMLDOMNode
Dim NearbyNode              As IXMLDOMNode
Dim Node                    As IXMLDOMNode

    On Error GoTo errorHandler

    Dim Lat As Double   'Chicago
    Lat = 41.878114
    Dim Lng As Double
    Lng = -87.629798

    Request.Open "GET", "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=" & Lat & "," & Lng & "&radius=50&type=restaurant&key=AIzaSyA5nFPM_9Ss_X410c35WfoP_obP5UwppRU" & "&sensor=False"
    Request.send
    Results.LoadXML Request.responseText

    Set StatusNode = Results.SelectSingleNode("//status")

    Select Case UCase(StatusNode.Text)

        Case "OK"
            For Each Node In Results.SelectNodes("//PlaceSearchResponse/result/name")
                Debug.Print Node.nodeTypedValue
            Next

        Case "ZERO_RESULTS"
            Debug.Print "The address does not exists"

        Case Else
            Debug.Print "Error"

    End Select

errorHandler:
    Set StatusNode = Nothing
    Set NearbyNode = Nothing
    Set Results = Nothing
    Set Request = Nothing
End Sub

我将
Lat,Lng
更改为类型
double
,并修改了
Request.Open“GET”
字符串

Sub Nearby()

Dim Request                 As New XMLHTTP30
Dim Results                 As New DOMDocument30
Dim StatusNode              As IXMLDOMNode
Dim NearbyNode              As IXMLDOMNode
Dim Node                    As IXMLDOMNode

    On Error GoTo errorHandler

    Dim Lat As Double   'Chicago
    Lat = 41.878114
    Dim Lng As Double
    Lng = -87.629798

    Request.Open "GET", "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=" & Lat & "," & Lng & "&radius=50&type=restaurant&key=AIzaSyA5nFPM_9Ss_X410c35WfoP_obP5UwppRU" & "&sensor=False"
    Request.send
    Results.LoadXML Request.responseText

    Set StatusNode = Results.SelectSingleNode("//status")

    Select Case UCase(StatusNode.Text)

        Case "OK"
            For Each Node In Results.SelectNodes("//PlaceSearchResponse/result/name")
                Debug.Print Node.nodeTypedValue
            Next

        Case "ZERO_RESULTS"
            Debug.Print "The address does not exists"

        Case Else
            Debug.Print "Error"

    End Select

errorHandler:
    Set StatusNode = Nothing
    Set NearbyNode = Nothing
    Set Results = Nothing
    Set Request = Nothing
End Sub

此函数返回逗号分隔的值字符串(尽管需要更多的错误捕获):


此函数返回逗号分隔的值字符串(尽管需要更多的错误捕获):


非常感谢。但是没有办法用函数获取这些信息?我有很多地址,所以我会更容易有一个功能。谢谢!但是没有办法用函数获取这些信息?我有很多地址,所以对我来说有一个函数会更容易。