Re: Consume Geodata web service -
01-04-2008
, 01:15 AM
Sub Test_GoogleGeoRequest()
Dim addr As String
addr = "11 Place Antonin Poncet, Lyon, FR"
'addr = "5 High Street, Galway, IE"
'addr = "41 Mill St,Marion,NY"
'addr = "Stanley St,Belleville,ON"
If Not MakeGeoRequest(addr) Then
MsgBox "Request failed", vbCritical, "F'd Up Request"
Exit Sub
End If
If HasData Then
Debug.Print Address 'formatted address
Debug.Print CountryNameCode
Debug.Print AdministrativeAreaName 'state name if US address
Debug.Print SubAdministrativeAreaName 'county name if US address
Debug.Print PostalCode 'zip code if US address
Debug.Print Latitude, Longitude
Else
MsgBox "Status Code: " & StatusCode & vbCrLf _
& "No information was found for" & vbCrLf & addr, _
vbInformation, "Address Was Not Found"
End If
End Sub
<GoogleGeoRequest Module>
Option Compare Database
Option Explicit
Const Map_Api_Key = "yourKeyHere"
Private m_Xml As String
Private doc As MSXML2.DOMDocument
'Private doc As Object
Public Property Get RawXML() As String
RawXML = m_Xml
End Property
Public Property Get CountryNameCode() As String
CountryNameCode =
GetNodeText("//Placemark/AddressDetails/Country/CountryNameCode")
End Property
Public Property Get Address() As String
Address = GetNodeText("//Placemark/address")
End Property
Public Property Get AdministrativeAreaName() As String
AdministrativeAreaName = _
GetNodeText("//Placemark/AddressDetails/Country/AdministrativeArea/AdministrativeAreaName")
End Property
Public Property Get SubAdministrativeAreaName() As String
SubAdministrativeAreaName = _
GetNodeText("//Placemark/AddressDetails/Country/AdministrativeArea/SubAdministrativeArea/SubAdministrativeAreaName")
End Property
Public Property Get PostalCode() As String
PostalCode = _
GetNodeText("//Placemark/AddressDetails/Country/AdministrativeArea/SubAdministrativeArea/PostalCode/PostalCodeNumber")
End Property
Public Property Get StatusCode() As String
StatusCode = GetNodeText("//Status/code")
End Property
Public Property Get HasData() As Boolean
HasData = StatusCode = "200"
End Property
Public Property Get Coordinates() As String
Coordinates = GetNodeText("//Placemark/Point/coordinates")
End Property
Public Property Get Latitude() As String
Latitude = Split(Coordinates, ",")(1)
End Property
Public Property Get Longitude() As String
Longitude = Split(Coordinates, ",")(0)
End Property
Public Function MakeGeoRequest(Address As String) As Boolean
Dim msXml As MSXML2.XMLHTTP
Set msXml = New MSXML2.XMLHTTP
'Dim msXml As Object
'Set msXml = CreateObject("Microsoft.XMLHTTP")
On Error GoTo errHandler
msXml.Open "GET", "http://maps.google.com/maps/geo?q=" & Address &
"&output=xml&key=" & Map_Api_Key, False
msXml.setRequestHeader "Content-Type", "text/html"
msXml.send
'Set doc = New MSXML2.DOMDocument
Set doc = CreateObject("MSXML2.DOMDocument")
m_Xml = msXml.ResponseText
doc.loadXML m_Xml
MakeGeoRequest = True
Exit Function
errHandler:
'return false
End Function
Private Function GetNodeText(path As String) As String
Dim n As IXMLDOMNode
'Dim n As Object
Set n = doc.selectSingleNode(path)
If Not n Is Nothing Then
GetNodeText = n.Text
End If
End Function
</GoogleGeoRequest Module> |