Google在Excel中使用VBA放置API - 想要根据关键字输入和起始位置返回附近位置的地址

时间:2017-06-07 17:54:51

标签: excel-vba google-places-api vba excel

我是编码的新手。我正在开发一个excel电子表格,我将几十个位置定义为“纬度,经度”,对于每个输入,我想根据关键字找到最近商店的地址(即“Staples”,“ CVS“等等。

我根据我在本论坛以及其他一些地方提取的内容编写了一些代码,但无论我输入什么内容,结果都显示为“-1”。

我认为问题可能在于我如何定义输出“regex.Pattern =”“”value“”。*?([0-9] +)“当我写VBA时,这对我有用使用谷歌的距离API。理想情况下,我想要的是最近的单个位置的PlaceID或lat,长坐标。

这是我的代码(我从下面的代码中省略了我的API密钥,但在我的版本中有它):

Public Function GetPlace(location As String, keyword As String)
    Dim firstVal As String, secondVal As String, thirdVal As String, lastVal 
As String
    firstVal = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
    secondVal = "&location="
    thirdVal = "&keyword="
    lastVal = "&rankby=distance&sensor=false&key=[my API key]"
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = firstVal & secondVal & Replace(location, " ", "+") & thirdVal & 
    Replace(keyword, " ", "+") & lastVal
    objHTTP.Open "GET", URL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
    If InStr(objHTTP.responseText, """place"" : {") = 0 Then GoTo ErrorHandl
    Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = """value"".*?([0-9]+)": regex.Global = False
    Set matches = regex.Execute(objHTTP.responseText)
    tmpVal = Replace(matches(0).SubMatches(0), ".", 
    Application.International(xlListSeparator))
    GetPlace = CDbl(tmpVal)
    Exit Function
ErrorHandl:
    GetPlace = -1
End Function

0 个答案:

没有答案