How to Integrate UK Postcode

Here is the exact script I use now, it works perfectly. If this doesn’t work on your system, there is something else going on.

function Read(postcode)
{
   var country = 'GB'
   var urlfmt = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:'+postcode;
   if(country != undefined) 
     urlfmt += '|country:'+country;
   var content = web.Download(urlfmt);
   var obj = JSON.parse(content);
   var lat = obj.results[0].geometry.location.lat;
   var lng = obj.results[0].geometry.location.lng;

   var addrurl = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&latlng=' + lat + ',' + lng + '&sensor=false'
   var addr = web.Download(addrurl);
   var addrObject = JSON.parse(addr);

   var data = GetAddressComponents(addrObject.results,postcode);
   if(data == null) return postcode;

   var street = ReadComponent('route',data);
   if(street == '-')
     street = ReadComponent('locality',data);
   if(street == '-')
     street = ReadComponent('administrative_area_level_4',data);
   var town = ReadComponent('postal_town',data);
   var county = ReadComponent('administrative_area_level_2',data);

   return postcode + ',' + street + ',' + town + ',' + county;
}

function GetAddressComponents(results,postcode)
{
    for(i=0;i < results.length;i++)
    {
       for(j=0; j< results[i].address_components.length;j++)
       {
         var component = results[i].address_components[j];
         if(component.long_name.replace(' ','') == postcode.replace(' ',''))
           return results[i].address_components;
       }
    }
    return null;
}

function ReadComponent(name,components)
{
   for(i=0;i<components.length;i++)
   {
      var component = components[i];
      
      for(j=0;j<component.types.length;j++)
      {
         if(component.types[j] == name)
           return component.long_name;
      } 
   }
   
   return '-';
}

Replace YOUR_API_KEY with your API Key.

To test the script, enter this into the test field in the bottom right: Read('EH1 2NG') (change with your postcode if you wish)

image

You should get a popup message with the address. If you get anything else, post it here and we can give suggestions.

image

Also I note you are using Windows 7, I remember a while ago I had an issue with JScript working (albeit it was on Windows Server 2003 if I recall). The system had an outdated (default) version of JScript and I needed to update it. I found this out by just testing the script like I suggest you do above.

1 Like