Postcode Integration for UK

thanks a lot, you are a hero, have a good day boss

This hasnt worked lately, i’ve made a key on google, how do i update the script as the current one returns
{
“error_message” : “Keyless access to Google Maps Platform is deprecated. Please use an API key with all your API calls to avoid service interruption. For further details please refer to http://g.co/dev/maps-no-account”,
“results” : [],
“status” : “OVER_QUERY_LIMIT”
}

You would need to check API documentation for sample request. You most likely need to sent API key along with the postcode in request Jon data.

Postcode integration configuration task is now broken, it no longer works because Google blocked non authenticated access to their API. (that is what they mean by “keyless access” in the error message)

To make it work, you need to get an API key from Google Maps:

Note Google will ask for billing details, this is unfortunately a new thing since July 2018, they charge for using the API but will give you $200 usage per month for free. Your usage should be well within this, but there is ways to ensure it does not go over this by restricting usage to $200, it is mentioned in their pricing FAQ pages.

Then you need to edit the script in Automation > Scripts, “Postcode API” script

There are 2 URLs, you need to change them both to use HTTPS (change http:// to https://). (Google do not allow use of the API over HTTP anymore).

You then need to add your API key (which you will get once you register above) to each of the URLs.

Change the first line with the URL to

var urlfmt = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:'+postcode;

and the second one to

var addrurl = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&latlng=' + lat + ',' + lng + '&sensor=false';

Replace YOUR_API_KEY in both URLs with the API key you get from Google.

6 Likes

For Distance calculation (miles) following script was used:

How would you translate below line with API key

var u = ‘http://maps.googleapis.com/maps/api/distancematrix/json?origins=‘+origin+’.&destinations=‘+destination+’.&mode=driving&language=en-GB&sensor=false&units=metric’;

Thanks…

var u = 'https://maps.googleapis.com/maps/api/distancematrix/json?key=YOUR_API_KEY&origins=’+origin+’.&destinations=’+destination+’.&mode=driving&language=en-GB&sensor=false&units=metric';

That is, I changed http:// to https:// and added key=YOUR_API_KEY parameter to the URL.

Note you may also have to login to Google Maps API Console (just google that term) and ensure you have added the Geocoding API to your Project.

3 Likes

I have tried to do a test but it returned : Expected’;’

This is the script I have changed

function Read(postcode)
{
   var YOUR_API_KEY = 'google-api-key';
   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 '-';
}

The if on line 7 doesnt look right to me… should have { } arround the depended code surely?
The line formatting sugests its just the one line after;

if(country != undefined){
     urlfmt += '|country:'+country;
    }
   var content = web.Download(urlfmt);
   var obj = JSON.parse(content);

But without actually working out whats happening and checking cant say if thats the cause of your error but think its likely.

ALTHOUGH, thats what emre posted and other ifs are the same so much be acceptable shorthand…

Saying that, you not testing correctly.
Should use the functioln name in test so something like;

Read('SW1 1AA')

Not that in matters how many functions but in your case you have multiple function, so you need to tell it which fuction to test with the values…

Its not the error i would have expected but probably a factor in your issue testing.

2 Likes

No it’s fine, an if statement without braces { } is allowed in Javascript if you only have one line of code within it.

https://forum.sambapos.com/t/how-to-call-distance-between-two-points-on-google-maps-api/4518?u=morshed1

I’m still having small bug with getting the distance between to point to function. This feature was working before implementing the required API key. The Postcode API and GoogleDistance scripts works.
Previously when Post code was entered distance would appear in the distance field now something is broken.

1 Like

Hi all,
Any help on this would be appreciated. I have tried varies things and still can not get distance to be displayed between two points (Original and delivery address) correctly. The post code API functions correctly because the correct distance is displayed on the map. However, incorrect Distance is calculated.

image

Incorrect distance is displayed:

Actual Distance:

Show the actual update distance action. Pretty sure your not actually updating the entity as you need to tell it what entity for entity name so would need to carry the field into the rule and use ENTITY NAME:Customer

Also, do you really want to call API and update entity on every order added? I would make it a once per ticket at Max API request.

Actions:

image

Rules:
image

Also, do you really want to call API and update entity on every order added? I would make it a once per ticket at Max API request.

This is only called for Take Away orders (for Delivery tickets)

Yes but it’s still called every time you add an order, surly you would be better to use ticket entity changed event to update.

So first thing to check would be to add a show message action with the script call to check the API call is returning what you expect.

Original setup is based on following, distance calculation was working fine. only changes made was to add the API key.

https://forum.sambapos.com/t/how-to-call-distance-between-two-points-on-google-maps-api/4518/30?u=morshed1

GoogleDistance Script:

var u = ‘https://maps.googleapis.com/maps/api/distancematrix/json?**key=YOUR_API_KEY**&amp;origins=’+origin+’.&amp;destinations=’+destination+’.&amp;mode=driving&amp;language=en-GB&amp;sensor=false&amp;units=metric’;

Postcode API script:

var urlfmt = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:'+postcode;
var addrurl = 'https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&latlng=' + lat + ',' + lng + '&sensor=false';

Ok, so you could just ignore me then…
Add a show message action calling the script and confirm the script is returning the value expected.
Im not convinced your rule is actually updating the entity, using a show message will help show if either its returning that value and is or its returning the lower value your expecting and not updating.

how to get access to your post @morshed1 - I have tired but it is showing “Sorry, you don’t have access to that topic!”

It’s a brta group topic.