Is there any API for calculating distance on the basis of UK postcodes. IF there is any, how can we call that in Service Now?what are the steps to call custom API in Service Now.

Chandresh
Tera Guru

Hello All,

I want to calculate distance on the basis of two postcodes of UK. Is there any API present for it.

Also, how can I call custom APIs in Service Now. In Postcodes.io   lot of APIs are present but I don't know how to use them in Service Now. Any ideas?

Regards/Chandresh

1 ACCEPTED SOLUTION

What about the following? You could add the "Location" reference field to tickets and "Current Location" reference field to Users. You would then define Postcodes as Locations, which have longitude and latitude information OOB. If you activate the GeoLocations plugin, it should even populate the long/lat values automatically from the postcodes. If they don't, you should write your own Business Rule script to populate the coordinates.



Once you have the coordinates filled in, you could create a new "Tickets nearby" module for your users, which is a list of records on the Incident table (or whatever table your tickets are in). The module would have a filter that would measure the distance between the Ticket's location and the User's current location. This functionality can be found in GeoLocations as well, as the Script Include called GeolocationUtils, which has the GeolocationUtils prototype with the function "getTwoPointsDistance: function(p1Lat, p1Long, p2Lat, p2Long)".


View solution in original post

12 REPLIES 12

What about the following? You could add the "Location" reference field to tickets and "Current Location" reference field to Users. You would then define Postcodes as Locations, which have longitude and latitude information OOB. If you activate the GeoLocations plugin, it should even populate the long/lat values automatically from the postcodes. If they don't, you should write your own Business Rule script to populate the coordinates.



Once you have the coordinates filled in, you could create a new "Tickets nearby" module for your users, which is a list of records on the Incident table (or whatever table your tickets are in). The module would have a filter that would measure the distance between the Ticket's location and the User's current location. This functionality can be found in GeoLocations as well, as the Script Include called GeolocationUtils, which has the GeolocationUtils prototype with the function "getTwoPointsDistance: function(p1Lat, p1Long, p2Lat, p2Long)".


Hi Peter,



Thank you so much, it worked like magic. however, there are little difference between distance calculated by the Geolocation plugin and distance calculated by Google Map. I believe it might be because in Service Now we are calculating the distance by below formula and Google Map use navigation/GPS satellite technology. Do you have any idea about this?



getTwoPointsDistance: function(p1Lat, p1Long, p2Lat, p2Long){


  var R = 6371; // Radius of the Earth in km


  var dLat = (p2Lat - p1Lat) * Math.PI / 180;


  var dLon = (p2Long - p1Long) * Math.PI / 180;


  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +


  Math.cos(p1Lat * Math.PI / 180) * Math.cos(p2Lat * Math.PI / 180) *


  Math.sin(dLon / 2) * Math.sin(dLon / 2);


  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));


  var d = R * c;


  return d; // returns the distance between p1 and p2 in kilometer


  },


It seems the GeoLocation plugin uses the Haversine formula to calculate the distance between two points as explained here. If you want to calculate the actual routed distance, you have to script it yourself and use the DistanceMatrixService from the Google Maps Javascript API. The DistanceMatrixResponseElement contains a distance field with the information as explained on stackoverflow http://stackoverflow.com/a/2472669/1410402. Sample code from Google can be found on their website Distance Matrix service Sample.



I don't know how popular your SN Instance is, but keep in mind that there are Google API limitations as explained here, JavaScript API Usage Limits.


Below is a quote of Google's JavaScript API Usage Limits:



Users of the standard API:


  • Free until exceeding 25,000 map loads per 24 hours for 90 consecutive days

Enable pay-as-you-go billing to unlock higher quotas:


After exceeding the free usage limits, billing at $0.50 USD / 1000 additional requests, up to 1,000,000 per 24 hours.