Search code examples
vbageocodegoogle-geocoder

Code to Get GPS Coordinates from Address (VB/VB.Net/VBA/VBScript)


I believe the Google API allows you to get coordinates for a given address. However, most (or should I say all) of the examples I've found aren't for vb. Usually it's some javascript example that just leaves me confused.

Here's some code I have that uses a geocoding service. This works great. It's just that I want to query Google Maps directly myself.

Public Function fgGetLatAndLongUsingAddress(sAddress As String) As String

    'This function works best with a complete address including the zip code
    Dim sResponseText As String, sReturn As String

    sReturn = "none"

    Dim objHttp As Object, sQuery As String
    sQuery = "http://rpc.geocoder.us/service/csv?address=" & Replace(sAddress, " ", "+")
    Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
    objHttp.Open "GET", sQuery, False
    objHttp.send
    sResponseText = objHttp.ResponseText
    gsLastLatLongResponseText = sResponseText
    Set objHttp = Nothing


    If Len(sResponseText) > 0 Then
        If InStr(sResponseText, "Bad Request") > 0 Then
            'Do Nothing
        ElseIf InStr(sResponseText, "couldn't find this address") > 0 Then
            'Do Nothing
        Else
            If InStr(sResponseText, vbCrLf) > 0 Then
                'We got more than one result
            End If
            If InStr(sResponseText, ",") > 0 Then
                Dim aryInfo() As String
                aryInfo = Split(sResponseText, ",")
                sReturn = aryInfo(0) & "," & aryInfo(1)
            End If
        End If
    End If


    fgGetLatAndLongUsingAddress = sReturn

End Function

Solution

  • This should do the job. You need to add a reference to the MSXML6 library (Microsoft XML, v6.0) via Tools > References in Excel

    Option Explicit
    
    Function getGoogleMapsGeocode(sAddr As String) As String
    
    Dim xhrRequest As XMLHTTP60
    Dim sQuery As String
    Dim domResponse As DOMDocument60
    Dim ixnStatus As IXMLDOMNode
    Dim ixnLat As IXMLDOMNode
    Dim ixnLng As IXMLDOMNode
    
    
    ' Use the empty string to indicate failure
    getGoogleMapsGeocode = ""
    
    Set xhrRequest = New XMLHTTP60
    sQuery = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
    sQuery = sQuery & Replace(sAddr, " ", "+")
    xhrRequest.Open "GET", sQuery, False
    xhrRequest.send
    
    Set domResponse = New DOMDocument60
    domResponse.loadXML xhrRequest.responseText
    Set ixnStatus = domResponse.selectSingleNode("//status")
    
    If (ixnStatus.Text <> "OK") Then
        Exit Function
    End If
    
    Set ixnLat = domResponse.selectSingleNode("/GeocodeResponse/result/geometry/location/lat")
    Set ixnLng = domResponse.selectSingleNode("/GeocodeResponse/result/geometry/location/lng")
    
    getGoogleMapsGeocode = ixnLat.Text & ", " & ixnLng.Text
    
    End Function
    

    The only real differences to your example are:

    • changing the URL and parameters of the query to use the Google API
    • treating the response as an XML document and using XPath to extract the required results

    Obviously there's no error handling whatsoever in my code but it should give you an idea of what you need to use the API. You definitely want to consult the API documentation yourself to find out about rate limiting etc