How to Integrate UK Postcode

Hi I have got API key but I can not find where I can insert this key on samba pos v5 can some help step by step. I looked on automation and scripts where it says API but I can see option where I can insert this API Key
Thanks

What API? Google?
There is a config task to setup google API but think they may have updated their API since that was created.
If its another source you will need to use scripts to request from the API.
We need more detail to offer suggestions to move forward.

UK postcode, I use to search address by inserting post code since 3 month stopped working then I look forum it says Google blocked for not authorized API so I had to create account on Google and got API key

@nepchef you are reposting this question that has already been given a solution. You asked the same thing in September and I gave you the solution.

Yes, and you follow my instructions as per last time, insert the API key and it works.

2 Likes

Thank you for reply but unfortunately it is not working. Here are my scripts, I have changed alphabets on API key but the rest is exactly the same I have just copied and pest could you please advise me where I am doing wrong

function Read(postcode)
{
var country = ‘GB’
var urlfmt = 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaPyB1BSMXMzTh8rEMZm6kAI2paIszNaFMzY&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=AIzaPyB1BSMXMzTh8rEMZm6kAI2paIszNaFMzY&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 ‘-’;


}

Entities Type as follows on postcode field

Editing Format : (GIR ?0AA|A-PR-UWYZ ?[0-9][ABD-HJLNP-UW-Z]{2})

Value : {CALL:postCode.Read(’$1’)}

Value Mappings : PostCode=$1;Street=$4;Town=$3;County=$2

If you test the script directly with a sample postcode does it return any error?

1 Like

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

Look at what mark posted, your not testing the script correctly.

I understand and I did test again looks fine but it dose not work.

Enter after typing in postcode?
Also double check with the postcode you used in test or test that one… Your test postcode has space, other doesn’t, may not matter but you have to rule out possibilities.

I have tried without space and also used different postcode as you can see on screen shot. test looks fine but when I type postcode on screen, it dose not search address.


Are you entering/moving to next field, it won’t be a live search, it will need submitting.

I’d take a guess that during all your trying to get it to work, you have altered the default setup on the Customers entity.

This is what it should be:

Editing Format: (GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})

Values: {CALL:postCode.Read('$1')}

Value Mappings: PostCode=$1;Street=$2;Town=$3;County=$4

If it works in the script editor and not on the entity, 100% you changed something as it works fine for me.

So just copy & paste the correct values I posted above then I am sure it will work.

It will work exactly like this, you don’t need to press enter after typing postcode and it doesn’t matter if you have a space in the middle or not.

Also, if the postcode has no result from Google Maps API, it will just do nothing on the entity screen. So that could also be the reason. So please test it with a known postcode like EH1 2NG.

2 Likes

Thank you it is now working, appreciate your help, instruction you have provided step by step with patiently,was useful

2 Likes

You can integrate the Postcode and Streetname from any CSV-File.
I use this in Paket Service-Network CallerID (NCID) as html mask for Customer save and edit.

1 Like

That’s a really nice setup there. However for UK unfortunately the postcode data is not free, it is licensed from Royal Mail (the post office) so you can’t get “official” data for free.

The best method currently is using Google Maps API as it is essentially free, at least for the usage in SambaPOS for a typical restaurant or takeaway. It will give street name and city from the postcode but not house number. There are third party services that can give data including house number selection but those all charge per use (typically £0.02-0.05 per lookup)

1 Like

Can you do this Offline search? Because I have a internet problem.

I guess this system is offline. How can we do offline search job by postcode.

You can’t - it uses service from Google Maps for postcode lookup, that’s an online service.