Hi Guys,
I know I have done this before but i cant locate that file now, but here's what i want help with.
What i want is i have a userform with a textbox1 on it when i click command button 1, it should search the address ....like if i put 125 main st...i want a listbox to be dynamically added to the userform with all the possible formatted addresses listed in it...currently it only gives me one address
Function G_LATLNG( _
InputLocation As Variant, _
Optional n As Long = 4, _
Optional Requery As Boolean = False _
) As Variant
' Requires a reference to Microsoft XML, v6.0
' The parameter 'n' refers to the type of reponse
' n = 1: Returns latitude, longitude as string
' n = 2: Returns latitude as double
' n = 3: Returns longitude as double
' n = 4: Returns address as string
' Updated 30/10/2012 to
' - return an #N/A error if an error occurs
' - cache only if necessary
' - check for and attempt to correct cached errors
' - work on systems with comma as decimal separator
Dim myRequest As XMLHTTP60
Dim myDomDoc As DOMDocument60
Dim addressNode As IXMLDOMNode
Dim latNode As IXMLDOMNode
Dim lngNode As IXMLDOMNode
Dim statusNode As IXMLDOMNode
Dim CachedFile As String
Dim NoCache As Boolean
Dim V() As Variant
On Error GoTo exitRoute
G_LATLNG = CVErr(xlErrNA) ' Return an #N/A error in the case of any errors
ReDim V(1 To 4)
' Check and clean inputs
If WorksheetFunction.IsNumber(InputLocation) _
Or IsEmpty(InputLocation) _
Or InputLocation = "" Then GoTo exitRoute
InputLocation = URLEncode(CStr(InputLocation), True)
' Check for existence of cached file
CachedFile = Environ("temp") & "\" & InputLocation & "_LatLng.xml"
NoCache = (Len(Dir(CachedFile)) = 0)
Set myRequest = New XMLHTTP60
If NoCache Or Requery Then ' if no cached file exists or if asked to requery then query Google
' Read the XML data from the Google Maps API
myRequest.Open "GET", "http://maps.googleapis.com/maps/api/geocode/xml?address=" _
& InputLocation & "&sensor=false", False
myRequest.Send
Else ' otherwise query the cached file
myRequest.Open "GET", CachedFile
myRequest.Send
Set myDomDoc = New DOMDocument60
myDomDoc.LoadXML myRequest.responseText
' Get the status code of the cached XML file in case of previously cached errors
Set statusNode = myDomDoc.SelectSingleNode("//status")
If statusNode.Text <> "OK" Then
Call G_LATLNG(InputLocation, n, True) ' Recursive way to try to remove cached errors
End If
End If
' Make the XML readable using XPath
Set myDomDoc = New DOMDocument60
myDomDoc.LoadXML myRequest.responseText
' If statusNode is "OK" then get the values to return
Set statusNode = myDomDoc.SelectSingleNode("//status")
If statusNode.Text = "OK" Then
' Get the location as returned by Google
Set addressNode = myDomDoc.SelectSingleNode("//result/formatted_address")
' Get the latitude and longitude node values
Set latNode = myDomDoc.SelectSingleNode("//result/geometry/location/lat")
Set lngNode = myDomDoc.SelectSingleNode("//result/geometry/location/lng")
V(1) = latNode.Text & "," & lngNode.Text
V(2) = Val(latNode.Text) ' Fixed for systems with comma as decimal separator
V(3) = Val(lngNode.Text) ' Fixed for systems with comma as decimal separator
V(4) = addressNode.Text
G_LATLNG = V(n)
If NoCache Then: Call CreateFile(CachedFile, myRequest.responseText) ' Cache API response if required
End If
exitRoute:
' Tidy up
Set latNode = Nothing
Set lngNode = Nothing
Set myDomDoc = Nothing
Set myRequest = Nothing
End Function
Public Function URLEncode( _
StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String
' Function from http://www.tinyguru.com/error/qid218181.html
Dim StringLen As Long: StringLen = Len(StringVal)
Dim i As Long, CharCode As Integer
Dim Char As String, Space As String
If StringLen > 0 Then
ReDim result(StringLen) 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, 61, 95, 123, 125, 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
'========================================================================================
Function CreateFile(fileName As String, Contents As String) As String
' Function from http://www.jpsoftwaretech.com/vba/create-new-text-documents-using-vba/
' creates file from string contents
Dim tempFile As String
Dim nextFileNum As Long
nextFileNum = FreeFile
tempFile = fileName
Open tempFile For Output As #nextFileNum
Print #nextFileNum, Contents
Close #nextFileNum
CreateFile = tempFile
End Function
Bookmarks