About This Code
Brief Description:
Geocoding class in Lotusscript
Contributor:
Karl-Henry Martinsson
Last Modified:
13 Jul 2009
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
This Lotusscript class is using the Google REST API for geocoding an address.
You simply instantiate the class by passing the address broken down into four arguments (street, city, state, zip). If you lack data, pass an empty string.
Note: The code is currently written for US addresses.
The class will submit the address to Google and get information back. The class exposes the same information you sent (street, city, state and zip) as well as latitude and longitude and "accuracy", a number describing how accurate the latitude/longitude is (street level, intersection, city, etc).
The class can be used not only to retrieve the latitude and longitude for a location, but also to verify that a particular address exists, or look up the zip code based on the street, city and state.
Simply take the code in GeoCode.txt and paste it into a (new) script library. I call it Class.GeoCode. Then you can use it everywhere you like.
I am working on an updated/expanded version that will let you generate KML data for Google Maps/Google Earth as well.
Usage / Example
Example 1.
This will simply display the data returned in a message box.
Dim geodata As GeoData
Set geodata = New GeoData("600 North State Highway 161", "Irving", "tx", "")
If geodata.IsValid Then
Msgbox geodata.Street & Chr$(13) & geodata.City & ", " & geodata.State & " " & geodata.ZIP,, "Accuracy = " & geodata.Accuracy
Msgbox "Lat: " & geodata.Latitude & " Longitude: " & geodata.Longitude
End If
Example 2.
This is code you can put in a button on a form. It reads a number of fields containing address info, call the GeoCode class, and then update the address fields with the data return by the class.
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim geodata As GeoData
Dim streetStr As String
Dim cityStr As String
Dim stateStr As String
Dim zipStr As String
Dim ret As Integer
Set uidoc = ws.CurrentDocument
streetStr = uidoc.FieldGetText("Address")
cityStr = uidoc.FieldGetText("City")
stateStr = uidoc.FieldGetText("State")
zipStr = uidoc.FieldGetText("ZIP")
Set geodata = New GeoData(streetStr, cityStr, stateStr, zipStr)
If geodata.HasAddInfo(streetStr) Then
ret = Msgbox("Possible apartment/suit detected. Value may get overwritten." & Chr$(13) & Chr$(13) & _
"Original address:" & Chr$(13) & StreetStr & Chr$(13) & CityStr & ", " & StateStr & " " & ZipStr & _
Chr$(13) & Chr$(13) & "Address returned by Google:" & Chr$(13) & geodata.Street & Chr$(13) & _
geodata.City & ", " & geodata.State & " " & geodata.ZIP & Chr$(13) & Chr$(13) & _
"Do you want to use this new address?",4+16,"Verify Address")
If ret = 7 Then
Exit Sub
End If
Else
If streetStr<>geodata.Street Or cityStr<>geodata.City Or stateStr<>geodata.State Or zipStr<>geodata.ZIP Then
ret = Msgbox("Original address:" & Chr$(13) & StreetStr & Chr$(13) & CityStr & ", " & StateStr & " " & ZipStr & _
Chr$(13) & Chr$(13) & "Address returned by Google:" & Chr$(13) & geodata.Street & Chr$(13) & geodata.City & _
", " & geodata.State & " " & geodata.ZIP & Chr$(13) & Chr$(13) &
"Do you want to use this new address?",4+32,"Verify Address")
If ret = 7 Then
Exit Sub
End If
End If
End If
Call uidoc.FieldSetText("Address", geodata.Street)
Call uidoc.FieldSetText("City", geodata.City)
Call uidoc.FieldSetText("State", geodata.State)
Call uidoc.FieldSetText("ZIP", geodata.ZIP)
Call uidoc.FieldSetText("Latitude", geodata.Latitude)
Call uidoc.FieldSetText("Longitude", geodata.Longitude)