+ Reply to Thread
Results 1 to 11 of 11

VBA Formula to Calculate Distances with Google Maps API

  1. #1
    Registered User
    Join Date
    10-07-2015
    Location
    Idaho Falls, Idaho
    MS-Off Ver
    2013
    Posts
    28

    VBA Formula to Calculate Distances with Google Maps API

    Hello,

    I am trying to get a formula I found to work for a project I am working on. The suggested VBA code is;

    'Calculate Google Maps distance between two addresses
    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

    Found on http://www.analystcave.com/excel-cal...een-addresses/

    I am using Chrome and I notice that it specifically states several web browsers in the text. Is that the issue? How can I get it to work with Chrome? The error I get is "Compile Error: Invalid Outside Procedure

    Regards,

    Tony

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: VBA Formula to Calculate Distances with Google Maps API

    it works for me??

    I dont believe the browser you use should have any bearing, you're doing this in Excel which means it doesnt touch your browser at all (if anything it may use some form of IE code but Im not sure).

    What exactly did you do to utilise this code? ideally can you post your workbook?

    All i did was add it into a new module and then use the function as detailed.

    Out of interest, you have actually got the ' at the start of the very first line havent you? Try removing that "Calculate Google......"line altogether.
    If someone has helped you then please add to their Reputation

  3. #3
    Registered User
    Join Date
    10-07-2015
    Location
    Idaho Falls, Idaho
    MS-Off Ver
    2013
    Posts
    28

    Re: VBA Formula to Calculate Distances with Google Maps API

    I removed that line but am still getting an error of #value!. I have included a link to the file below as it is too large to post in the thread. Any help would be appretiated.

    https://www.dropbox.com/s/5if8mlri2i...Data.xlsm?dl=0

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: VBA Formula to Calculate Distances with Google Maps API

    you havent put the variables in quotation marks........change it to
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-07-2015
    Location
    Idaho Falls, Idaho
    MS-Off Ver
    2013
    Posts
    28

    Re: VBA Formula to Calculate Distances with Google Maps API

    Thank you! It works perfectly now. You are my hero. Saved me hours of manual searching!

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Formula to Calculate Distances with Google Maps API

    It'll also stop working. Google are pretty stringent on their use of APIs and regularly stop access to those who break the terms and conditions (which this does). Bing has no such stipulations providing you have a licence key, see: http://www.excelforum.com/tips-and-t...rivetimes.html

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Formula to Calculate Distances with Google Maps API

    PS Please use code tags when posting code

  8. #8
    Registered User
    Join Date
    10-07-2015
    Location
    Idaho Falls, Idaho
    MS-Off Ver
    2013
    Posts
    28

    Re: VBA Formula to Calculate Distances with Google Maps API

    How does it break the terms of use?

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Formula to Calculate Distances with Google Maps API

    Quote Originally Posted by Someone at Google;
    Use of the Google Maps Distance Matrix API must relate to the display of information on a Google Map; for example, to determine origin-destination pairs that fall within a specific driving time from one another, before requesting and displaying those destinations on a map. Use of the service in an application that doesn't display a Google map is prohibited. For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions.
    https://developers.google.com/maps/d...x/usage-limits

  10. #10
    Registered User
    Join Date
    10-07-2015
    Location
    Idaho Falls, Idaho
    MS-Off Ver
    2013
    Posts
    28

    Re: VBA Formula to Calculate Distances with Google Maps API

    Thanks Kyle! I appreciate the information.

  11. #11
    Registered User
    Join Date
    12-30-2012
    Location
    NL
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA Formula to Calculate Distances with Google Maps API

    Anyone any experience with adding optional parameters such as "arrival_time" "traffic_model"?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Calculating Google Maps Distances Using Postcodes
    By smart_as in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 05-14-2021, 03:08 PM
  2. Using google to calculate distances… in a macro
    By fosters_ in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-15-2014, 03:37 PM
  3. Replies: 1
    Last Post: 10-18-2013, 03:58 PM
  4. Broken Formula to Hyperlink longitude and latitude cells to google maps
    By Nola ADA in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-09-2013, 09:48 AM
  5. Calculate Distance using Google Maps
    By excelinexcel7 in forum Excel General
    Replies: 2
    Last Post: 12-06-2012, 06:19 PM
  6. Returning Distances From Google Maps
    By lauzhang in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2012, 10:00 AM
  7. Replies: 1
    Last Post: 12-15-2011, 10:42 PM

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