+ Reply to Thread
Results 1 to 18 of 18

Calculating Google Maps Distances Using Postcodes

Hybrid View

  1. #1
    Registered User
    Join Date
    02-28-2011
    Location
    Bournemouth, England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Calculating Google Maps Distances Using Postcodes

    Hello. I have read many threads on this site about using Google Maps to calculate the distance in miles between 2 different post codes. I have copied the relevant code but cannot get it to return anything other than a zero.

    I want to return in cell C the value of the distance in miles between the post code in cell A and cell B. Any help would be greatly appreciated.

    I have attached the workbook in question.
    Attached Files Attached Files
    Last edited by smart_as; 07-05-2011 at 11:10 AM.

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

    Re: Calculating Google Maps Distances Using Postcodes

    You might try changing:

    strSearch = strSearch & ":"""
    to:

    strSearch = strSearch & ":'"
    (the above resides in the 3rd Function listed)

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Calculating Google Maps Distances Using Postcodes

    For that purpose I use:
    (with a reference to Microsoft XML library)
    Sub google_distance_snb()
      c02 = "6721MZ+Nederland"
      c03 = "1072NC+Nederland"
    
    ' c02=activesheet.cells(1,1)
    ' c03=activesheet.cells(1,2)  
    
      With New MSXML.XMLHTTPRequest
        .Open "Get", "http://maps.google.nl/maps?f=d&source=s_d&saddr=" & c02 & "&daddr=" & c03
        .send
    
        Do
          DoEvents
        Loop Until .ReadyState = 4
            
        ActiveWorkbook.Sheets(1).Cells(1, 3) = Split(Split(.responseText, "distance:'")(1), "'")(0)
        .abort
      End With
    End Sub
    Last edited by snb; 07-02-2011 at 06:22 AM.



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

    Re: Calculating Google Maps Distances Using Postcodes

    @snb, per my earlier post I believe the response string has altered, using "distance:" as search criteria will no longer retrieve the appropriate value.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Calculating Google Maps Distances Using Postcodes

    @DonkeyOte,

    You are quite right.
    I amended my previous code.

  6. #6
    Registered User
    Join Date
    02-28-2011
    Location
    Bournemouth, England
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Calculating Google Maps Distances Using Postcodes

    Thank you so much that corrected the problem. I really appreciate this.

    Jon

  7. #7
    Registered User
    Join Date
    01-19-2012
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Calculating Google Maps Distances Using Postcodes

    Hello Jon,
    I am also looking into this file. Is there a possibility to post on here the working file. The one that has been corrected? I have tried to downoad your earlier version and tried to follow the changes from sbn but could not make it work
    Would you be happy to post your working file on here or I can give you an email address?
    Thanks for your help Jon
    Didier

  8. #8
    Registered User
    Join Date
    02-07-2012
    Location
    Internet
    MS-Off Ver
    None
    Posts
    13

    Re: Calculating Google Maps Distances Using Postcodes

    Hello,

    I would also like to see the working version. I have tried to get this thing working for about 2 days now and I am starting to think its not actually possible.

    Thanks.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Calculating Google Maps Distances Using Postcodes

    Apparently not possible (Google seems to have a 'always change what works' policy):

    Sub snb()
     c01 = "4522EG + Nederland"
     c02 = "3571NC + Nederland"
        
     With New XMLHTTPRequest
      .Open "Get", "http://maps.google.nl/maps?f=d&source=s_d&saddr=" & c01 & "&daddr=" & c02
      .send
      Do
       DoEvents
      Loop Until .readyState = 4
                    
      c03 = .responsetext
      .abort
     End With
        
     If InStr(c03, "km</span>") <> 0 Then
      c03 = Mid(c03, InStr(c03, "km</span>") - 6, 40)
      If Val(c03) = 0 Then c03 = Mid(c03, 2)
      If Val(c03) = 0 Then c03 = Mid(c03, 2)
            
      c04 = Split(c03, "<")(0)
      c03 = Split(Split(c03, ">")(2), "<")(0)
      MsgBox "Distance: " & c04 & String(2, vbLf) & "Duration: " & c03
     End If
    End Sub
    NB. you need a reference to Microsoft XML version 2.0
    Attached Files Attached Files
    Last edited by snb; 02-07-2012 at 10:21 AM.

  10. #10
    Registered User
    Join Date
    02-07-2012
    Location
    Internet
    MS-Off Ver
    None
    Posts
    13

    Re: Calculating Google Maps Distances Using Postcodes

    Where do I need to add this code?

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Calculating Google Maps Distances Using Postcodes

    Whereever you want to as long as it's a codemodule (workbook, sheet, userform, macromodule, classmodule).

    I added an attachment to the previous post.
    Last edited by snb; 02-07-2012 at 10:22 AM.

  12. #12
    Registered User
    Join Date
    02-07-2012
    Location
    Internet
    MS-Off Ver
    None
    Posts
    13

    Re: Calculating Google Maps Distances Using Postcodes

    Your attachment works perfectly however whenever I try it for one that is in the UK it does not seem to work.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Calculating Google Maps Distances Using Postcodes

    so give me those postcodes and I'll try it over here.
    I know it works for Barcelona, Sweden etc.

  14. #14
    Registered User
    Join Date
    02-07-2012
    Location
    Internet
    MS-Off Ver
    None
    Posts
    13

    Re: Calculating Google Maps Distances Using Postcodes

    Need the distance in miles between ss11 8bh & cm11 1hd

  15. #15
    Registered User
    Join Date
    10-14-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Calculating Google Maps Distances Using Postcodes

    So how did you get on, any chance of the working version for all UK post codes?

  16. #16
    Registered User
    Join Date
    02-13-2014
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Calculating Google Maps Distances Using Postcodes

    I'm a basic Excel user. How do I use code above? where do I put?
    thanks

  17. #17
    Registered User
    Join Date
    05-28-2019
    Location
    Newport
    MS-Off Ver
    365
    Posts
    30

    Re: Calculating Google Maps Distances Using Postcodes

    Hello all
    i know this is a deadpost but can we reopen it.
    i think the coding in this workbook is great however i cannot see how it has been resolved.

    I am also stuck with a similar position, i do not want to start looking at APIs when it comes to extracting data from googlemaps, i would like to look at other options and this should nail it on the head tbh

    Mario

  18. #18
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Calculating Google Maps Distances Using Postcodes

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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