Closed Thread
Results 1 to 43 of 43

Google Maps Driving Times

Hybrid View

  1. #1
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Google Maps Driving Times

    hey thegooser123

    found this by Shred Dude at VbaX
    'shred dude vbax
    Public Function getGoogDistanceTime(startAddr As String, startCity As String, _
        startState As String, startZip As String, endAddr As String, _
        endCity As String, endState As String, endZip As String) As String
         
        Dim sURL As String
        Dim BodyTxt As String
         
        sURL = "http://maps.google.com/maps?f=d&source=s_d&saddr="
        sURL = sURL & Replace(startAddr, " ", "+") & ",+" & Replace(startCity, " ", "+") & ",+" & startState
        sURL = sURL & "&daddr=" & Replace(endAddr, " ", "+") & ",+" & Replace(endCity, " ", "+") & ",+" & endState
        sURL = sURL & "&hl=en"
         
        BodyTxt = getHTML(sURL)
         
        If InStr(1, BodyTxt, "distance:""") = 0 Then getGoogDistanceTime = "Error": Exit Function
         
        getGoogDistanceTime = parseGoog("distance", BodyTxt) & " / " & parseGoog("time", BodyTxt)
         
    End Function
     
    Public Function getHTML(strURL As String) As String
         'Returns the HTML code underlying a given URL
        Dim oXH As Object
        Set oXH = CreateObject("msxml2.xmlhttp")
        With oXH
            .Open "get", strURL, False
            .send
            getHTML = .responseText
        End With
        Set oXH = Nothing
    End Function
    
    Public Function parseGoog(strSearch As String, strHTML As String) As String
    strSearch = strSearch & ":"""
    If InStr(1, strHTML, strSearch) = 0 Then parseGoog = "Not Found": Exit Function
    parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
    parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, """") - 1)
    End Function
    hope it helps
    Last edited by DonkeyOte; 02-06-2011 at 05:58 AM. Reason: edit: added parseGoog which was missing from original
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  2. #2
    Registered User
    Join Date
    08-30-2010
    Location
    Cornwall, UK
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Google Maps Driving Times

    Thanks for the help

    How would I implement this into my spreadsheet? I know how to insert it as code but lost as to what to do after that. Where would I put the Start and finish point?

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Google Maps Driving Times

    Given you wish to return distance & time separately you would need to modify the first function slightly, eg:

    'shred dude vbax
    Public Function getGoogDistanceTime(rngSAdd As Range, rngEAdd As Range, Optional strReturn As String = "distance") As String
        Dim sURL As String
        Dim BodyTxt As String
        sURL = "http://maps.google.com/maps?f=d&source=s_d"
            sURL = sURL & "&saddr=" & Replace(rngSAdd(1).Value, " ", "+")
            sURL = sURL & "&daddr=" & Replace(rngEAdd(1).Value, " ", "+")
            sURL = sURL & "&hl=en"
        BodyTxt = getHTML(sURL)
        If InStr(1, BodyTxt, strReturn, vbTextCompare) = 0 Then
            getGoogDistanceTime = "Error"
        Else
            getGoogDistanceTime = parseGoog(strReturn, BodyTxt)
        End If
    End Function
    then

    A3: 
    =GetGoogDistanceTime($A$1,$A$2,"time")
    
    A4
    =GetGoogDistanceTime($A$1,$A$2,"distance")

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1