Has anyone had any success with doing a postcode lookup from a web client. The sample code on first glance looks relatively straight forward but I have spent what seems like a year and a day without any success. I have attached their sample code which builds an http request with xml and returns with the data and an instruction to fire another method. I would be really grateful of just a point in the right direction.
HTML
<head>
<title>Address finder (Fast Address)</title>
<script>
var account_code='A123';
var license_code='B456';
function pcaFastAddressBegin()
{
var postcode = document.forms[0]["searchpostcode"].value;
var building = document.forms[0]["searchbuilding"].value;
var scriptTag = document.getElementById("pcaScriptTag");
var headTag = document.getElementsByTagName("head").item(0);
var strUrl = "";
//Build the url
strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
strUrl += "&action=fetch";
strUrl += "&postcode=" + escape(postcode);
strUrl += "&building=" + escape(building);
strUrl += "&account_code=" + escape(account_code);
strUrl += "&license_code=" + escape(license_code);
strUrl += "&callback=pcaFastAddressEnd";
//Make the request
if (scriptTag)
{
//The following 2 lines perform the same function and should be interchangeable
headTag.removeChild(scriptTag);
//scriptTag.parentNode.removeChild(scriptTag);
}
scriptTag = document.createElement("script");
scriptTag.src = strUrl
scriptTag.type = "text/javascript";
scriptTag.id = "pcaScriptTag";
headTag.appendChild(scriptTag);
}
function pcaFastAddressEnd()
{
//Test for an error
if (pcaIsError)
{
//Show the error message
alert(pcaErrorMessage);
}
else
{
//Check if there were any items found
if (pcaRecordCount==0)
{
alert("Sorry, no matching items found");
}
else
{
document.forms[0]["line1"].value = '' + pca_line1[0];
document.forms[0]["line2"].value = '' + pca_line2[0];
document.forms[0]["line3"].value = '' + pca_line3[0];
document.forms[0]["line4"].value = '' + pca_line4[0];
document.forms[0]["line5"].value = '' + pca_line5[0];
document.forms[0]["town"].value = '' + pca_post_town[0];
document.forms[0]["county"].value = '' + pca_county[0];
document.forms[0]["postcode"].value = '' + pca_postcode[0];
}
}
}
</script>
</head>
<body>
<form>
<table bgcolor="#eeeeee" border="0" width="100%" cellpadding="4" cellspacing="2">
<tr>
<td bgcolor="#ffffff"><b>Building</b></td>
<td valign=top>
<input name="searchbuilding" size ="30">
</td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Postcode</b></td>
<td valign=top>
<input name="searchpostcode" size ="30">
<input type="button" value="Find" onclick="Javascript:pcaFastAddressBegin()" id=btnFind name=btnFind>
</td>
</tr>
<tr>
<td colspan=2>
<div id="divLoading" style="display:none">Loading. Please wait...</div>
<select name="selectaddress" style="display:none"></select>
<input type="button" value="Select" onclick="Javascript:pcaFetchBegin()" id=btnFetch name=btnFetch style="display:none">
</td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Line1</b></td>
<td valign=top><input name="line1" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Line2</b></td>
<td valign=top><input name="line2" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Line3</b></td>
<td valign=top><input name="line3" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Line4</b></td>
<td valign=top><input name="line4" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Line5</b></td>
<td valign=top><input name="line5" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Town</b></td>
<td valign=top><input name="town" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>County</b></td>
<td valign=top><input name="county" size ="30"></td>
</tr>
<tr>
<td bgcolor="#ffffff"><b>Postcode</b></td>
<td valign=top><input name="postcode" size ="10"></td>
</tr>
</table>
</form>
</body>
HTML
I’ve just tested it and the code works fine. You need to register an account at Postcode Anywhere if you haven’t already, and create a license key (my account > popups and licenses > add a key). You’ll be given an account code when you register. Add these to the code and it should spring into action.
BTW, if you use the dummy postcode WR2 6NJ while testing, you can perform as many free test address lookups as you want.
The code I above is fine and works as you say off an html page - however I want PCA to populate Servoy fields and not an HTML form. Are you saying I put the whole lot into an HTML area within Servoy?
I have figured out that I need to populate Servoy fields instead of HTML form fields. I also understand that the code builds an http request. I’m a little lost with the removeChild/appendChild but believe this is to form the XML. But the bit that I can’t get my head around is how to fire the script on the return.
Great tip on the dummy postcode - this will come in useful.
nickbarnett:
The code I above is fine and works as you say off an html page - however I want PCA to populate Servoy fields and not an HTML form. Are you saying I put the whole lot into an HTML area within Servoy?
I have figured out that I need to populate Servoy fields instead of HTML form fields. I also understand that the code builds an http request. I’m a little lost with the removeChild/appendChild but believe this is to form the XML. But the bit that I can’t get my head around is how to fire the script on the return.
Great tip on the dummy postcode - this will come in useful.
Hi Nick,
Any luck with the postcodes?
I’m having sort of a similar problem and would appreciate help, thank you.
HI,
This will most likely only work in the Netherlands, but we look up road-distances through a service (webservices.nl, commercial, you need an account)), we pass it two postcodes, and receive back the road-distance fastest route for use of calculating travel-expences. To my knowledge (in Europe) only the UK and the Netherlands have a postcode-system fine-grained enough for this purpose.
Next step might be to get this info through Google.
Anyone any experiences with that ?
Dexadrine came up with this a few years back.
Still works for us. I bow to his greatness.
var vAccount = "AccountNumber";
//alternate license use
if(globals.gPostcodeAnywhereLicCount == 0){
// var vLicense = "LICENCEKEY";// license key
var vLicense = "ALT-LICENCEKE"; //license key
var vMachine = "MachineID"; //machine id for this license
globals.gPostcodeAnywhereLicCount = 1;
}
else if(globals.gPostcodeAnywhereLicCount == 1){
// var vLicense = "LICENCEKEY"; //license key
var vLicense = "ALT-LICENCEKEY";//license key
var vMachine = "MachineID"; //machine id for this license
globals.gPostcodeAnywhereLicCount = 0;
}
var vSearchPostcode = globals.gPostcodeSearch;
var vBuilding = globals.gBuilding;
if(!vSearchPostcode) return;
//Build up the URL to send the request to.
var vUrl = "http://services.postcodeanywhere.co.uk/tsv.aspx?";
vUrl += "account_code=" + vAccount;
vUrl += "&license_code=" + vLicense;
vUrl += "&machine_id=" + vMachine;
vUrl += "&action=fetch";
if(vBuilding){ vUrl += "&building="; + escape(vBuilding); }
vUrl += "&style=simple"; //simple or rawgeographic
vUrl += "&postcode=" + escape(vSearchPostcode); //vSearchPostcode.replace(' ', '%');
//application.output(vUrl)
var vPageData = plugins.http.getPageData(vUrl); //put the page data into a var
if(!vPageData) return;
// Create a new record in usage log
forms.TABLE_postcodeanywhere_log.controller.newRecord();
forms.TABLE_postcodeanywhere_log.licence = vLicense;
forms.TABLE_postcodeanywhere_log.machine = vMachine;
forms.TABLE_postcodeanywhere_log.use_date = new Date();
forms.TABLE_postcodeanywhere_log.usetype = 'UserType';
//application.output(vPageData);
var rowArray = vPageData.split("\r");
var valueListView = new Array();
var valueListReal = new Array();
for ( var i = 1 ; i < rowArray.length ; i++ )
{
if(!utils.stringTrim(rowArray[i])) {continue;}
var rowData = rowArray[i].split("\t");
var vAddressLineView = '';
var vAddressLineReal = '';
for ( var x = 2 ; x <= 11 ; x++ )
{
vAddressLineReal += rowData[x];
if(x <11) {vAddressLineReal += '|';}
if(!rowData[x]) {continue;}
vAddressLineView += rowData[x];
if(x <11) {vAddressLineView += '|';}
}
valueListView.push(vAddressLineView);
valueListReal.push(vAddressLineReal);
}
application.setValueListItems( 'address_search',valueListView,valueListReal);
It then uses .split on “|” to populate individual fields.
Hope this helps.
With much thanks to Patrick at Dr Maison, here is the solution I have working at the moment using the XML Reader plugin - I haven’t got round to optimising it. Also take a look at Postcode Software ( http://www.postcodesoftware.net ) as they are less expensive on the lookups. We will probably swap to this provider when our initial license runs outs.
There are 2 methods:
pcaOrderAddress - this method finds the addresses available and presents them in a form-in-dialogue box.
function pcaOrderAddress()
{
var ordeid = orde_id
// acquire the adresses from postcode anywhere
var xmla = plugins.http.getPageData ('http://services.postcodeanywhere.co.uk/xml.aspx?account_code=' + globals.pca_account + '&license_code=' + globals.pca_license + '&action=lookup&type=by_postcode&postcode=' + escape(postcode));
// create xml object from resulting xml data
var xmlb = plugins.XML.XMLDocument(xmla)
// acquire root element - postcodeanywhere
var vRoot = xmlb.rootElement;
var vItems = vRoot.getDescendants('Item');
if (vItems && vItems.length > 0)
{
for ( var i = 0 ; i < vItems.length ; i ++ )
{
var vItem = vItems[i];
// The items in your example have no values, only attributes so we need these
var vItemAttributes = vItem.getAttributes();
orde_to_pcaw.newRecord(false)
var vSingleAttribute = vItemAttributes[0];
orde_to_pcaw.pcaw_reference = vSingleAttribute.value;
var vSingleAttribute = vItemAttributes[1];
orde_to_pcaw.sequence = vSingleAttribute.value;
var vSingleAttribute = vItemAttributes[2];
orde_to_pcaw.description = vSingleAttribute.value;
}
application.showFormInDialog( 'postcodes_table_form', 50, 50, 450, 450, 'Select Postal Address', false, false, 'pca', true)
currentcontroller.find()
forms.pcaw_tbl_orde.orde_id = ordeid
currentcontroller.search()
}
}
selectAddress - this method is attached to the address line in the form-in-dialogue and populates the fields from the selected address. I create the records in a scratch table and use a relationship which uses the users id. At the end of the process, I delete all of the scratch records. Be aware that Potscode Anywhere delivers the address as an attribute and not as data.
function selectAddress()
{
// acquire the adresses from postcode anywhere
var xmla = plugins.http.getPageData ('http://services.postcodeanywhere.co.uk/xml.aspx?account_code=' + globals.pca_account + '&license_code=' + globals.pca_license + '&action=fetch&id=' + escape(pcaw_reference) + '&style=rawgeographic');
// create xml object from resulting xml data
var xmlb = plugins.XML.XMLDocument(xmla)
// acquire root element - postcodeanywhere
var vRoot = xmlb.rootElement;
var vItems = vRoot.getDescendants('Item');
if (vItems && vItems.length > 0)
{
pcaw_to_orde.address_1 = '';
pcaw_to_orde.address_2 = '';
pcaw_to_orde.address_3 = '';
pcaw_to_orde.address_4 = '';
pcaw_to_orde.city = '';
pcaw_to_orde.county = '';
pcaw_to_orde.grid_east = null;
pcaw_to_orde.grid_north = null;
for ( var i = 0 ; i < vItems.length ; i ++ )
{
var vItem = vItems[i];
// The items in your example have no values, only attributes so we need these
var vItemAttributes = vItem.getAttributes();
for ( var j = 0 ; j < vItemAttributes.length ; j ++ )
{
var vSingleAttribute = vItemAttributes[j];
var vSingleAttributeName = vSingleAttribute.name
switch(vSingleAttributeName)
{
case 'line1':
pcaw_to_orde.address_1 = vSingleAttribute.value;
break;
case 'line2':
pcaw_to_orde.address_2 = vSingleAttribute.value;
break;
case 'line3':
pcaw_to_orde.address_3 = vSingleAttribute.value;
break;
case 'line4':
pcaw_to_orde.address_4 = vSingleAttribute.value;
break;
case 'post_town':
pcaw_to_orde.city = vSingleAttribute.value;
break;
case 'county':
pcaw_to_orde.county = vSingleAttribute.value;
break;
case 'grid_east_m':
pcaw_to_orde.grid_east = vSingleAttribute.value;
break;
case 'grid_north_m':
pcaw_to_orde.grid_north = vSingleAttribute.value;
break;
default:
break;
}
}
}
}
forms.pcaw_tbl_orde.controller.deleteAllRecords()
application.closeFormDialog('pca')
}