将Google Map API密钥添加到距离计算功能

时间:2016-01-06 20:28:21

标签: api google-maps-api-3

我有以下代码并已注册并从谷歌收到API密钥但不知道在哪里放置密钥。任何人都可以帮我解决密钥的位置吗?

Distance_Calc
Public Function GetDistance(start As String, dest As String)
    Dim firstVal As String, secondVal As String, lastVal As String
    firstVal = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="
    secondVal = "&destinations="
    lastVal = "&mode=car&language=pl&sensor=false"
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = firstVal & Replace(start, " ", "+") & secondVal & Replace(dest, " ", "+") & 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, """distance"" : {") = 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))
    GetDistance = CDbl(tmpVal)
    Exit Function
ErrorHandl:
    GetDistance = -1
End Function

1 个答案:

答案 0 :(得分:0)

您正在使用java中的Directions Matrix web service。如果是在客户端上,则不建议使用(并且很难将密钥保护为web service requires a "server key")。如果它在服务器上,请使用服务器密钥(并保护它),specifying it in the request

示例(from the documentation):

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&key=YOUR_API_KEY

在您的情况下,您可以将其添加到您附加到请求的lastVal(并删除不再需要的sensor参数):

lastVal = "&mode=car&language=pl&key=YOUR_API_KEY"

要在客户端上使用javascript中的路线矩阵,请使用Google Maps Javascript API Distance Matrix Service,然后使用browser key when you include the API