Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Google Places API(附近搜索)不返回结果_Excel_Vba_Google Places Api - Fatal编程技术网

Excel Google Places API(附近搜索)不返回结果

Excel Google Places API(附近搜索)不返回结果,excel,vba,google-places-api,Excel,Vba,Google Places Api,我从Excel中调用这个VBA函数,它使用GooglePlacesAPI附近的搜索函数,不返回结果。我知道会有结果,因为这个HTTP调用返回相同输入参数的结果。您知道函数为什么不返回结果吗 https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=45.5662453,-122.6628821&radius=1500&type=park&key=AIzaSyCbBAbRZG0yhCHjJLa

我从Excel中调用这个VBA函数,它使用GooglePlacesAPI附近的搜索函数,不返回结果。我知道会有结果,因为这个HTTP调用返回相同输入参数的结果。您知道函数为什么不返回结果吗

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=45.5662453,-122.6628821&radius=1500&type=park&key=AIzaSyCbBAbRZG0yhCHjJLaKjv8ARp2J6pv1wSQ
公共函数GetNearbyPark(纬度为双精度,经度为双精度,半径为整数)作为字符串
'-----------------------------------------------------------------------------------------------------
'此函数使用谷歌地图返回给定纬度、经度和半径的公园名称
'在搜索API附近放置。
“半径以米为单位
'-----------------------------------------------------------------------------------------------------
'声明必要的变量。
将apiKey设置为字符串
Dim xmlhttpRequest作为对象
Dim xmlDoc作为对象
Dim xmlStatusNode作为对象
Dim xmlNearbyParkNameNode作为对象
Dim xmlNearbyParkAddressNode作为对象
'在此变量中设置API密钥。
'这里是代码中唯一需要放置API密钥的地方。
apiKey=“aizasycbabrzg0yhchjjlakjv8arp2j6pv1wsq”
'检查是否已提供API密钥。
如果apiKey=vbNullString或apiKey=“API键”,则
GetNearbyPark=“空的或无效的API密钥”
退出功能
如果结束
'通用错误处理。
关于错误转到错误处理程序
'创建请求对象并检查它是否已成功创建。
设置xmlhttpRequest=CreateObject(“MSXML2.ServerXMLHTTP”)
如果xmlhttpRequest什么都不是,那么
GetNearbyPark=“无法创建请求对象”
退出功能
如果结束
'根据Google Places API创建请求。参数(来自Google页面):
“-经度
“-纬度
“-半径
'xmlhttpRequest.Open“GET”https://maps.googleapis.com/maps/api/geocode/xml?" _
“&”&address=“&Application.EncodeURL(地址)&”&key=“&apiKey,False
调试。打印“在API调用时”
xmlhttpRequest.Open“GET”https://maps.googleapis.com/maps/api/place/nearbysearch/xml?“&”location=“&”latitude“&”、“&经度&”&radius=“&radius&”&type=park&key=“&apiKey,False
'将请求发送到Google服务器。
xmlhttpRequest.send
'创建DOM文档对象并检查它是否已成功创建。
设置xmlDoc=CreateObject(“MSXML2.DOMDocument”)
如果xmlDoc什么都不是,那么
GetNearbyPark=“无法创建DOM文档对象”
退出功能
如果结束
'从请求读取XML结果。
xmlDoc.LoadXML xmlhttpRequest.responseText
'从状态节点获取值。
设置xmlStatusNode=xmlDoc.SelectSingleNode(//状态)
打印xmlStatusNode
'根据状态节点结果,相应地继续。
选择Case UCase(xmlStatusNode.Text)
案例“OK”API请求成功。
'至少返回了一个结果。
'获取第一个结果的公园名称和地址节点值。
设置xmlNearbyParkNameNode=xmlDoc.SelectSingleNode(//result/name)
'设置xmlNearbyParkAddressNode=xmlDoc.SelectSingleNode(//结果/邻近)
Debug.Print xmlNearbyParkNameNode
'以文本形式返回公园名称和地址
'GetNearbyPark=xmlNearbyParkNameNode.Text&',“&xmlNearbyParkAddressNode.Text
GetNearbyPark=xmlNearbyParkNameNode.Text
案例“ZERO_RESULTS”地理代码成功,但未返回任何结果。
GetNearbyPark=“定义坐标半径内不存在任何停车场”
“超过每日限额”表示以下情况之一:
“-API密钥丢失或无效。
“-尚未对您的帐户启用计费。
“-已超过自行设定的使用上限。
“-提供的付款方式不再有效
(例如,信用卡已过期)。
GetNearbyPark=“计费或付款问题”
案例“超过查询限制”请求者已超过配额限制。
GetNearbyPark=“超出配额限制”
案例“请求被拒绝”,API未完成请求。
GetNearbyPark=“服务器拒绝了请求”
案例“无效的_请求”API请求为空或格式不正确。
GetNearbyPark=“请求为空或格式不正确”
案例“未知错误”'由于服务器错误,无法处理请求。
GetNearbyPark=“未知错误”
以防万一,以防万一。。。
GetNearbyPark=“错误”
结束选择
'退出前(或出现错误时)释放对象。
错误处理程序:
设置xmlStatusNode=Nothing
设置xmlNearbyParkNameNode=Nothing
设置xmlNearbyParkAddressNode=Nothing
设置xmlDoc=Nothing
设置xmlhttpRequest=Nothing
端函数

您的问题没有正确/完整地呈现代码,但在顶部的链接中,“radius”前面有一个空格。如果我将整个内容粘贴到地址栏,我会得到“无效请求”。如果我删除空格,它会工作。

您的问题不会正确/完整地呈现代码,但在顶部的链接中,在“radius”之前有一个空格。如果我将整个内容粘贴到地址栏,我会得到“无效请求”。如果我删除了空格,它就会工作。

代码中存在大量错误,导致宏过早退出

除非您确切知道将返回哪些错误,否则使用错误处理程序永远都不是一个好主意

  • 调试的第一步应该是禁用错误处理程序。
    • 如果您这样做了,您会发现这些语句都是非法的,因为对象不支持该方法。您需要显式返回正确的文本
      'Object does not support this method
      'Debug.Print xmlStatusNode
      Debug.Print xmlStatusNode.Text
      
      ...
      
      'Object does not support this method
      'Debug.Print xmlNearbyParkNameNode
      Debug.Print xmlNearbyParkNameNode.Text
      
          'Remove quote marks from around "latitude" so as to send the variable and not the string
      xmlhttpRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?" & "location=" & latitude & "," & longitude & "&radius=" & Radius & "&type=park&key=" & apiKey, False
      
      'Since maximum radius is 50,000 must use Long data type
      Public Function GetNearbyPark(latitude As Double, longitude As Double, Radius As Long) As String
      
      =GetNearbyPark(45.5662453,-122.6628821,1500) --> Peninsula Park Rose Garden
      
      https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=latitude,-122.6628821&radius=1500&type=park&key=AIzaSyCbBAbRZG0yhCHjJLaKjv8ARp2J6pv1wSQ
      
      Option Explicit
      
      'Since maximum radius is 50,000 must use Long data type
      Public Function GetNearbyPark(latitude As Double, longitude As Double, Radius As Long) As String
      
      '-----------------------------------------------------------------------------------------------------
      'This function returns the park name for a given latitude and longitude and radius using the Google
      'Places Nearby Search API.
      'Radius is in meters
      '-----------------------------------------------------------------------------------------------------
      
      'Declaring the necessary variables.
      Dim apiKey                   As String
      Dim xmlhttpRequest           As Object
      Dim xmlDoc                   As Object
      Dim xmlStatusNode            As Object
      Dim xmlNearbyParkNameNode    As Object
      Dim xmlNearbyParkAddressNode As Object
         
      'Set your API key in this variable.
      'Here is the ONLY place in the code where you have to put your API key.
      apiKey = "AIzaSyCbBAbRZG0yhCHjJLaKjv8ARp2J6pv1wSQ"
      
      'Check that an API key has been provided.
      If apiKey = vbNullString Or apiKey = "The API Key" Then
          GetNearbyPark = "Empty or invalid API Key"
          Exit Function
      End If
      
      'Generic error handling.
      'Probably no need for this
      'On Error GoTo errorHandler
              
      'Create the request object and check if it was created successfully.
      Set xmlhttpRequest = CreateObject("MSXML2.ServerXMLHTTP")
      If xmlhttpRequest Is Nothing Then
          GetNearbyPark = "Cannot create the request object"
          Exit Function
      End If
          
      'Create the request based on Google Places API. Parameters (from Google page):
      '- Longitude
      '- Latitude
      '- Radius
      
      'xmlhttpRequest.Open "GET", "https://maps.googleapis.com/maps/api/geocode/xml?" _
      '& "&address=" & Application.EncodeURL(address) & "&key=" & apiKey, False
      
      Debug.Print "At API call"
      
      'Remove quote marks from around "latitude" so as to send the variable and not the string
      xmlhttpRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?" & "location=" & latitude & "," & longitude & "&radius=" & Radius & "&type=park&key=" & apiKey, False
      
      'Send the request to the Google server.
      xmlhttpRequest.send
      
      'Create the DOM document object and check if it was created successfully.
      Set xmlDoc = CreateObject("MSXML2.DOMDocument")
      If xmlDoc Is Nothing Then
          GetNearbyPark = "Cannot create the DOM document object"
          Exit Function
      End If
      
      'Read the XML results from the request.
      xmlDoc.LoadXML xmlhttpRequest.responseText
      
      'Get the value from the status node.
      Set xmlStatusNode = xmlDoc.SelectSingleNode("//status")
      
      'Object does not support this method
      'Debug.Print xmlStatusNode
      Debug.Print xmlStatusNode.Text
      
      
      'Based on the status node result, proceed accordingly.
      Select Case UCase(xmlStatusNode.Text)
      
          Case "OK"                       'The API request was successful.
                                          'At least one result was returned.
              
              'Get the park name and address node values of the first result.
              Set xmlNearbyParkNameNode = xmlDoc.SelectSingleNode("//result/name")
              'Set xmlNearbyParkAddressNode = xmlDoc.SelectSingleNode("//result/vicinity")
              
              'Object does not support this method
              'Debug.Print xmlNearbyParkNameNode
              Debug.Print xmlNearbyParkNameNode.Text
              
              'Return the park name and address as text
               'GetNearbyPark = xmlNearbyParkNameNode.Text & ", " & xmlNearbyParkAddressNode.Text
               GetNearbyPark = xmlNearbyParkNameNode.Text
          
          Case "ZERO_RESULTS"             'The geocode was successful but returned no results.
              GetNearbyPark = "No park exists within the radius of the defined coordinates"
              
          Case "OVER_DAILY_LIMIT"         'Indicates any of the following:
                                          '- The API key is missing or invalid.
                                          '- Billing has not been enabled on your account.
                                          '- A self-imposed usage cap has been exceeded.
                                          '- The provided method of payment is no longer valid
                                          '  (for example, a credit card has expired).
              GetNearbyPark = "Billing or payment problem"
              
          Case "OVER_QUERY_LIMIT"         'The requestor has exceeded the quota limit.
              GetNearbyPark = "Quota limit exceeded"
              
          Case "REQUEST_DENIED"           'The API did not complete the request.
              GetNearbyPark = "Server denied the request"
              
          Case "INVALID_REQUEST"           'The API request is empty or is malformed.
              GetNearbyPark = "Request was empty or malformed"
          
          Case "UNKNOWN_ERROR"            'The request could not be processed due to a server error.
              GetNearbyPark = "Unknown error"
          
          Case Else   'Just in case...
              GetNearbyPark = "Error"
          
      End Select
          
      'Release the objects before exiting (or in case of error).
      errorHandler:
          Set xmlStatusNode = Nothing
          Set xmlNearbyParkNameNode = Nothing
          Set xmlNearbyParkAddressNode = Nothing
          Set xmlDoc = Nothing
          Set xmlhttpRequest = Nothing
      
      End Function