vb Geo Coding with Google API

Function GeoCode(strAddress)
	Dim strURL
	Dim xmlhttp
	Dim strServerName
	Dim strKey
	Dim strGeoCodeResponse
	Dim GeoArray
	
	'get Server Name
	strServerName = lcase(Request.ServerVariables("SERVER_NAME"))
	strServerName = Replace(strServerName,"www.","") 'cater for both www and non-www URLs

	'set API Key according to Server Name
	Select Case strServerName
		Case "localhost"
			strKey = "abc123"
		Case "example.com"
			strKey = "123abc"
	End Select

	'format user input for use with GeoCode
	strAddress = MaxLength(strAddress,1024)
	strAddress = Replace(strAddress," ","+")

	'build URL to call
	strURL = "http://maps.google.com/maps/geo?q=" & strAddress & "&output=csv&gl=uk&key=" & strKey
	Response.Write(strURL & "<br />")
	
	'call URL
	Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
	xmlhttp.open "GET",strURL,false
	xmlhttp.send ""
	strGeoCodeResponse = xmlhttp.ResponseText
	Set xmlhttp = nothing
	
	'build array from GeoCode response
	GeoArray = Split(strGeoCodeResponse,",")
	
	'build response from last 2 items in array (latitude and longitude)
	strGeoCodeResponse = GeoArray(2) & "," & GeoArray(3)

	GeoCode = strGeoCodeResponse
End Function
Geo Coding an address using the Google API.

Updated: Monday 4th October 2010, 10:36pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.