+ Reply to Thread
Results 1 to 8 of 8

Google Maps Function to show Kilometers instead of meters.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2015
    Location
    Gothenburg
    MS-Off Ver
    2016
    Posts
    78

    Google Maps Function to show Kilometers instead of meters.

    Hey guys. I got this code from "TheAnalystCave" and think it worked quite well. The only things that confuses me is that I can't find how to change the value of distance. It shows meters to me but I want it to show Kilometers instead, any suggestions of where I can change that?

    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
    Public Function MultiGetDistance(ParamArray args() As Variant) As Double
        MultiGetDistance = 0
        Dim startLoc As String, endLoc As String, i As Long
        For i = LBound(args) To UBound(args) - 1
            startLoc = args(i): endLoc = args(i + 1)
            MultiGetDistance = MultiGetDistance + GetDistance(startLoc, endLoc)
        Next i
    End Function

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Google Maps Function to show Kilometers instead of meters.

    Use =Convert or change
    GetDistance = CDbl(tmpVal)
    to
    GetDistance = CDbl(tmpVal)/1000

  3. #3
    Registered User
    Join Date
    10-08-2015
    Location
    Gothenburg
    MS-Off Ver
    2016
    Posts
    78

    Re: Google Maps Function to show Kilometers instead of meters.

    Worked like a charm Kenneth! Thanks alot!
    Last edited by Challebjoern; 05-26-2016 at 07:40 AM.

  4. #4
    Registered User
    Join Date
    10-08-2015
    Location
    Gothenburg
    MS-Off Ver
    2016
    Posts
    78

    Re: Google Maps Function to show Kilometers instead of meters.

    Got any quick way to show the distance in a cell? Now I've made a button which I have to press to show it, I kinda want it to show in a cell below once i press enter when the destination is filled in.

    Quote Originally Posted by Kenneth Hobson View Post
    Use =Convert or change
    GetDistance = CDbl(tmpVal)
    to
    GetDistance = CDbl(tmpVal)/1000

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Google Maps Function to show Kilometers instead of meters.

    If you mean that you have the inputs in A and B columns and the UDF formula in say C then that uses A and B then it should work.

  6. #6
    Registered User
    Join Date
    10-08-2015
    Location
    Gothenburg
    MS-Off Ver
    2016
    Posts
    78

    Re: Google Maps Function to show Kilometers instead of meters.

    That's exactly what I mean. So I write destinations "From: in A1" and "To: in A3" and I want to show the distance "Total distance xx km: in A6". What formula do I use for that?

    Quote Originally Posted by Kenneth Hobson View Post
    If you mean that you have the inputs in A and B columns and the UDF formula in say C then that uses A and B then it should work.

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Google Maps Function to show Kilometers instead of meters.

    In A6:
    =GetDistance(A1, A3)

+ 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. VBA Route Calculator - Google Maps Api 22 Minutes quicker than Actual Google Website
    By lookingforhelp1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2015, 01:23 PM
  2. Distance and mileage function to use with google maps in excel 2007
    By thefiddler2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-03-2013, 11:52 AM
  3. Google Maps Info
    By kocheleknick in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-26-2012, 10:53 AM
  4. Google Maps integration
    By GeorgY in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2011, 10:51 PM
  5. Google Maps in Excel?
    By kklein in forum Excel General
    Replies: 8
    Last Post: 09-26-2011, 06:54 PM
  6. Use google maps to get distance
    By rdr910 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-13-2010, 09:16 PM
  7. Google maps in excel
    By censura in forum Excel General
    Replies: 0
    Last Post: 02-21-2008, 04:03 AM

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