Consulting

Page 3 of 3 FirstFirst 1 2 3
Results 41 to 51 of 51

Thread: Solved: Total distance and time between two cities in a state

  1. #41
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    That is what we get for getting older, things start breaking and falling apart...

    The xml output from the API has multiple roots so it does not lend itself well to standard xml parsing methods. I guess we could look at the json output but then we would probably just use our standard text parsing methods that are prone to failing again.

    I noticed that the return results are just a bit different now, for:
    10008 Falls Road Potomac MD 40854 500 EL Camino Real Santa Clara CA 95053.

    It was 2,833 mi / 1 day 21 hours but is now 2,830 mi / 1 day 22 hours.

    [vba]' Google API code directions:
    ' http://code.google.com/apis/maps/doc...on/directions/

    '=GoogleDistanceTime(A4,B4,C4,D4,E4,F4,G4,H4)
    Public Function GoogleDistanceTime(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 xml As String, sDistance As String, sDuration As String
    Dim sURL As String
    Dim s As String, _
    d As String, _
    t As String

    'http://maps.googleapis.com/maps/api/directions/xml?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklaho ma+City,OK&sensor=false
    sURL = "http://maps.googleapis.com/maps/api/directions/xml?origin="
    sURL = sURL & Replace(startAddr, " ", "+") & ",+" & Replace(StartCity, " ", "+") & ",+" & startState
    sURL = sURL & "&destination=" & Replace(endAddr, " ", "+") & ",+" & Replace(EndCity, " ", "+") & ",+" & endState
    sURL = sURL & "&sensor=false"

    Application.Volatile False

    xml = getXML(sURL)
    sDistance = pGoog("distance", xml)
    sDuration = pGoog("duration", xml)
    GoogleDistanceTime = sDistance & " / " & sDuration
    End Function

    Private Function getXML(strURL As String) As String
    Dim HTTPreq As Object
    Dim BodyTxt As String

    'Set HTTPreq = CreateObject("msxml2.xmlhttp")
    Set HTTPreq = CreateObject("WinHttp.WinHttpRequest.5.1")
    With HTTPreq
    .Open "GET", strURL, False
    .send
    .WaitForResponse
    BodyTxt = .responseText
    End With
    Set HTTPreq = Nothing
    getXML = BodyTxt
    End Function

    Private Function pGoog(strSearch As String, strHTML As String) As String
    Dim s As String
    s = pRevTags(strSearch, strHTML)
    pGoog = pRevTags("text", s)
    End Function

    Private Function pRevTags(strSearch As String, strHTML As String) As String
    Dim s As String, p1 As Long, p2 As Long, lss As Integer
    p1 = InStrRev(strHTML, "<" & strSearch & ">")
    If p1 = 0 Then
    pRevTags = "Not Found"
    Exit Function
    End If
    p2 = InStrRev(strHTML, "</" & strSearch & ">")
    lss = Len(strSearch)
    s = Mid(strHTML, p1 + lss + 2, p2 - p1 - 2 - lss)
    pRevTags = s
    End Function
    [/vba]
    Last edited by Kenneth Hobs; 12-30-2011 at 10:34 AM.

  2. #42
    VERY USEFUL!

  3. #43
    Hi Guys,

    My UDF is not working.. can someone please send me a file containing the UDF in working condition...

    Many Thanks,
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  4. #44
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    My code works fine. In your last file's L2:

    =GoogleDistanceTime(A2,B2,C2,D2,E2,F2,G2,H2)

  5. #45
    Thanks Kenneth,

    But it still not working, giving me Value error...

    Thanks,
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  6. #46
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you use your code, then I am not surprised. If you used mine, I would be surprised. Post a simple workbook with my code if you want me to check it out.

  7. #47
    Hi Kenneth,

    attach is the sample workbook for your reference.


    Many Thanks,
    Attached Files Attached Files
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  8. #48
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    It worked fine for me in Excel 2010. I3 and I4 results were:
    2,829 mi / 1 day 22 hours
    710 mi / 11 hours 50 mins

  9. #49
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Worked for me too, same results as Kenneth.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #50
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Even worked fine for my house to the Royal Academy London, without C, D, G or H values.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #51
    VBAX Newbie
    Joined
    Oct 2013
    Posts
    1
    Location

    listbox

    Quote Originally Posted by vishwakarma View Post
    Hi,

    Just try the code provided by Mr. Kenneth....

    I've attached the sheet provided by him.

Posting Permissions

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