+ Reply to Thread
Results 1 to 3 of 3

Getting data from XML data returned from Google Geocode API

  1. #1
    Registered User
    Join Date
    12-05-2012
    Location
    Crewe, Cheshire, UK
    MS-Off Ver
    Excel 2010
    Posts
    12

    Getting data from XML data returned from Google Geocode API

    I have been searching around for tips on using the google maps api's to get data into a workbook. Basically what I'm trying to do is, based on an address (or part of an address) input by the workbook user, get details of the Town/City, country etc.

    I've found all this information is provided by googleapis, using the http://maps.googleapis.com/maps/api/geocode api, but am having difficulty in getting the info back out of the xml file.

    This is the code i'm using (pieced together from various sources):


    Function GoogleGeocode(address As String) As String
    Dim strAddress As String
    Dim strQuery As String
    Dim strLatitude As String
    Dim strLongitude As String

    strAddress = URLEncode(address)

    strQuery = "http://maps.googleapis.com/maps/api/geocode/xml?"
    strQuery = strQuery & "address=" & strAddress
    strQuery = strQuery & "&sensor=false"
    Dim googleResult As New MSXML2.DOMDocument
    Dim googleService As New MSXML2.XMLHTTP

    googleService.Open "GET", strQuery, False
    googleService.send
    googleResult.LoadXML (googleService.responseText)

    Dim oNodes As MSXML2.IXMLDOMNodeList
    Dim oNode As MSXML2.IXMLDOMNode
    Set oNodes = googleResult.getElementsByTagName("address_component")
    For Each oNode In oNodes
    If Not oNode Is Nothing Then
    Debug.Print oNode.Text
    End If
    Next
    End Function

    Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String
    Dim StringLen As Long: StringLen = Len(StringVal)

    If StringLen > 0 Then
    ReDim result(StringLen) As String
    Dim i As Long, CharCode As Integer
    Dim Char As String, Space As String

    If SpaceAsPlus Then Space = "+" Else Space = "%20"

    For i = 1 To StringLen
    Char = Mid$(StringVal, i, 1)
    CharCode = Asc(Char)

    Select Case CharCode
    Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
    result(i) = Char
    Case 32
    result(i) = Space
    Case 0 To 15
    result(i) = "%0" & Hex(CharCode)
    Case Else
    result(i) = "%" & Hex(CharCode)
    End Select
    Next i
    URLEncode = Join(result, "")
    End If
    End Function


    Basically I've got this far, just need some tips on converting the data back into excel, especially when Google provides details for more than one match (which it does if the address is unclear)

    Try http://maps.googleapis.com/maps/api/...n&sensor=false - this gives an idea of the response that Google provides

    Really really appreciate any tips - I'm fairly experienced in VBA, but XML and tags etc. is all new to me

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

    Re: Getting data from XML data returned from Google Geocode API

    Unfortunately people won't be able to help you since this breaches the google terms and conditions. To use their geocode service, you must display the results on a map - you can't just return them to Excel

  3. #3
    Registered User
    Join Date
    12-05-2012
    Location
    Crewe, Cheshire, UK
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Getting data from XML data returned from Google Geocode API

    Kyle123, thanks for pointing that out to me.
    I didn't realize that was the case with the Geocode API's, as all that i'd learnt about it was from ....... yes, excel forums!!
    I am still interested in reading XML from excel VBA as it looks very useful function. Really appreciate any help

+ 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