Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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 如何在VBA中添加googleapi密钥_Excel_Vba_Google Maps_Google Maps Api 3 - Fatal编程技术网

Excel 如何在VBA中添加googleapi密钥

Excel 如何在VBA中添加googleapi密钥,excel,vba,google-maps,google-maps-api-3,Excel,Vba,Google Maps,Google Maps Api 3,我有一个excel文件,我正试图建立与谷歌地图API,以便我可以告诉地址之间的距离。由于限制,我一次只能运行几行。我确实有一个API密钥,但无法确定在何处应用该密钥。这是我的代码,我正在从一些开源在线编辑。如果我一次只做一行,代码就可以工作,但是由于有数百条记录,我需要复制和粘贴大量的行。所以我有谷歌的许可证密钥。有人能帮我弄清楚在哪里使用钥匙吗 先谢谢你 Const strUnits = "imperial" ' imperial/metric (miles/km)

我有一个excel文件,我正试图建立与谷歌地图API,以便我可以告诉地址之间的距离。由于限制,我一次只能运行几行。我确实有一个API密钥,但无法确定在何处应用该密钥。这是我的代码,我正在从一些开源在线编辑。如果我一次只做一行,代码就可以工作,但是由于有数百条记录,我需要复制和粘贴大量的行。所以我有谷歌的许可证密钥。有人能帮我弄清楚在哪里使用钥匙吗

先谢谢你

              Const strUnits = "imperial" ' imperial/metric (miles/km)

            Function CleanHTML(ByVal strHTML)
            'Helper function to clean HTML instructions
            Dim strInstrArr1() As String
            Dim strInstrArr2() As String
            Dim s As Integer

            strInstrArr1 = Split(strHTML, "<")
            For s = LBound(strInstrArr1) To UBound(strInstrArr1)
               strInstrArr2 = Split(strInstrArr1(s), ">")
               If UBound(strInstrArr2) > 0 Then
                    strInstrArr1(s) = strInstrArr2(1)
               Else
                    strInstrArr1(s) = strInstrArr2(0)
               End If
            Next

            CleanHTML = Join(strInstrArr1)
            End Function

            Public Function formatGoogleTime(ByVal lngSeconds As Double)
            'Helper function. Google returns the time in seconds, so this converts it into time format hh:mm

            Dim lngMinutes As Long
            Dim lngHours As Long

            lngMinutes = Fix(lngSeconds / 60)
            lngHours = Fix(lngMinutes / 60)
            lngMinutes = lngMinutes - (lngHours * 60)

            formatGoogleTime = Format(lngHours, "00") & ":" & Format(lngMinutes, "00")
            End Function




            Function gglDirectionsResponse(ByVal strStartLocation, ByVal strEndLocation, ByRef strTravelTime, ByRef strDistance, ByRef strInstructions, Optional ByRef strError = "") As Boolean
            On Error GoTo errorHandler
            ' Helper function to request and process XML generated by Google Maps.

            Dim strURL As String
            Dim objXMLHttp As Object
            Dim objDOMDocument As Object
            Dim nodeRoute As Object
            Dim lngDistance As Long

            Set objXMLHttp = CreateObject("MSXML2.XMLHTTP")
            Set objDOMDocument = CreateObject("MSXML2.DOMDocument.6.0")

            strStartLocation = Replace(strStartLocation, " ", "+")
            strEndLocation = Replace(strEndLocation, " ", "+")

            strURL = "http://maps.googleapis.com/maps/api/directions/xml" & _
                        "?origin=" & strStartLocation & _
                        "&destination=" & strEndLocation & _
                        "&sensor=false" & _
                        "&units=" & strUnits   'Sensor field is required by google and indicates whether a Geo-sensor is being used by the device making the request

            'Send XML request
            With objXMLHttp
                .Open "GET", strURL, False
                .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
                .Send
                objDOMDocument.LoadXML .ResponseText
            End With

            With objDOMDocument
                If .SelectSingleNode("//status").Text = "OK" Then
                    'Get Distance
                    lngDistance = .SelectSingleNode("/DirectionsResponse/route/leg/distance/value").Text ' Retrieves distance in meters
                    Select Case strUnits
                        Case "imperial": strDistance = Round(lngDistance * 0.00062137, 1)  'Convert meters to miles
                        Case "metric": strDistance = Round(lngDistance / 1000, 1) 'Convert meters to miles
                    End Select

                    'Get Travel Time
                    strTravelTime = .SelectSingleNode("/DirectionsResponse/route/leg/duration/value").Text  'returns in seconds from google
                    strTravelTime = formatGoogleTime(strTravelTime) 'converts seconds to hh:mm

                    'Get Directions
                    For Each nodeRoute In .SelectSingleNode("//route/leg").ChildNodes
                        If nodeRoute.BaseName = "step" Then
                            strInstructions = strInstructions & nodeRoute.SelectSingleNode("html_instructions").Text & " - " & nodeRoute.SelectSingleNode("distance/text").Text & vbCrLf
                        End If
                    Next

                    strInstructions = CleanHTML(strInstructions) 'Removes MetaTag information from HTML result to convert to plain text.

                Else
                    strError = .SelectSingleNode("//status").Text
                    GoTo errorHandler
                End If
            End With

            gglDirectionsResponse = True
            GoTo CleanExit


                If strError = "" Then strError = Err.Description
                strDistance = -1
                strTravelTime = "00:00"
                strInstructions = ""
                gglDirectionsResponse = False

                Set objDOMDocument = Nothing
                Set objXMLHttp = Nothing

            End Function


            Function getGoogleTravelTime(ByVal strFrom, ByVal strTo) As String
            'Returns the journey time between strFrom and strTo

            Dim strTravelTime As String
            Dim strDistance As String
            Dim strInstructions As String
            Dim strError As String

            If gglDirectionsResponse(strFrom, strTo, strTravelTime, strDistance, strInstructions, strError) Then
                getGoogleTravelTime = strTravelTime
            Else
                getGoogleTravelTime = strError
            End If

            End Function

            Function getGoogleDistance(ByVal strFrom, ByVal strTo) As String
            'Returns the distance between strFrom and strTo
            'where strFrom/To are address search terms recognisable by Google
            'i.e. Postcode, address etc.

            Dim strTravelTime As String
            Dim strDistance As String
            Dim strError As String
            Dim strInstructions As String

            If gglDirectionsResponse(strFrom, strTo, strTravelTime, strDistance, strInstructions, strError) Then
                getGoogleDistance = strDistance
            Else
                getGoogleDistance = strError
            End If

            End Function

            Function getGoogleDirections(ByVal strFrom, ByVal strTo) As String
            'Returns the directions between strFrom and strTo
            'where strFrom/To are address search terms recognisable by Google
            'i.e. Postcode, address etc.

            Dim strTravelTime As String
            Dim strDistance As String
            Dim strError As String
            Dim strInstructions As String

            If gglDirectionsResponse(strFrom, strTo, strTravelTime, strDistance, strInstructions, strError) Then
                getGoogleDirections = strInstructions
            Else
                getGoogleDirections = strError
            End If

            End Function

我今天刚从谷歌拿到API密钥

我不得不玩弄这些代码,但我使用的例程与您在上面的代码示例中使用的完全相同

应该注意的是,必须使用SSL发送请求。以下字符串中的https不是http:

strURL = "https://maps.googleapis.com/maps/api/directions/xml" & _
        "?origin=" & strStartLocation & _
        "&destination=" & strEndLocation & _
        "&sensor=false" & _
        "&units=" & strUnits
这里是我放置API密钥的地方,现在我的应用程序工作起来就像我需要密钥之前一样

希望这能解决你的问题

================================================

APIKey = "Your API Key Here!"

'Send XML request

With objXMLHttp

    .Open "GET", strURL & "&key=" & APIKey, False
    .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
    .send
    objDOMDocument.LoadXML .responseText
End With

这可能比接收应答所需包含的代码要多。看起来您在发布的代码中确实有一个键。谷歌的服务有配额和费率限制,你会遇到什么?您是否已为该密钥启用信用卡计费?谢谢。我确实有一个已启用的公司密钥。我在这里的挑战是我没有被调用的密钥,所以我确信我达到了免费版本配额。有没有一种方法可以让你看到我是如何添加我的密钥的,这样我就不会碰到免费版本配额的?对不起,让我补充一下,你在这里看到的是我尝试添加JSON调用而不是XML调用。但这是一个错误。因此,注释掉的XML调用就是实际运行的调用。我只是编辑了代码,以显示它在运行时的外观。这就是我需要尝试和理解如何向其中添加密钥的地方。很抱歉搞混了。先谢谢你。