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

syedfarhan
Kilo Sage

Hi chandresh,



You can use google maps to calculate the distance , if interested let me the share the code and UI action script.




Regards,


Syed


Hi Syed,



Thanks for the response that will be very helpful. Please share the code.


Hi Chandresh,



UI Action:



Name: Open Google Map


Table: Location [cmn_location] // Specify the table


Action name: open_google_map


Show insert: false


Client: true


Form link/List context menu: true


OnClick: openGoogleMap()


Condition: (!current.street.nil() && !current.city.nil()) || (!current.latitude.nil() && !current.longitude.nil())


Comments: Shows a google map icon on a location field if the location has a street and city listed.



function openGoogleMap() {


    //Retrieve the 'Location' record


    var sysId = typeof rowSysId == 'undefined' ? gel('sys_uniqueValue').value : rowSysId;


    var gr = new GlideRecord('cmn_location');


    gr.get(sysId);




    //Create and display the Map URL


    var mapURL = "http://maps.google.com/?q=";


    //Try location and city


    if(gr.street && gr.city){


          mapURL = mapURL + gr.street + ',' + gr.city + ',' + gr.state + ',' + gr.zip + ',' + gr.country;


    }


    //Else try latitude and longitude


    else if(gr.latitude && gr.longitude){


          mapURL = mapURL + gr.latitude + ',' + gr.longitude;


    }


    //Strip '#' symbols to avoid encoding errors


    mapURL = mapURL.replace(/#/g, "");


    window.open(mapURL);


}


Once the UI Action is created, you should be able to right-click a location record from a list or click the 'Open Google Map' link.




UI Macro:



Name: open_google_map


Description:Shows a google map icon on a location field if the location has a street and city listed.


Activate by adding the attribute: ref_contributions=open_google_map to a location reference field



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


    <g:evaluate var="jvar_guid" expression="gs.generateGUID(this);" />


    <j:set var="jvar_n" value="open_google_map${jvar_guid}:${ref}"/>




    <g2:evaluate var="jvar_show_google_map_display" jelly="true">


          var id = __ref__.getSysIdValue();


          if (id == null)


                "none";


          else {


                var loc = new GlideRecord('cmn_location');


                loc.get(id);


                if ((!loc.street.nil() $[AND] !loc.city.nil()) || (!loc.latitude.nil() $[AND] !loc.longitude.nil()))


                      "";


                else


                      "none";


          }


    </g2:evaluate>




    <a id="${jvar_n}"


    onclick="openGoogleMap('${ref}')"


    name="${jvar_n}"


    style="display:$[jvar_show_google_map_display]"


    title="${gs.getMessage('Open a Google Map for this location')}">


            <img border="0" src="https://maps.gstatic.com/favicon2.ico" height="16" width="16"/>     </a>




    <script>


          needsRefreshLoc = false;


          function onChange_location_show_google_map(element, original, changed, loading) {


                var s = '${ref}'.split('.');


                var referenceField = s[1];


                if (needsRefreshLoc == false) {


                      needsRefreshLoc = true;


                      return;


                }


                if (changed.length == 0) {


                      $('${jvar_n}').hide();


                      return;


                }


                var locRec = g_form.getReference(referenceField, locationGoogleMapReturn);


          }


         


          function locationGoogleMapReturn(locRec) {


                var e = $('${jvar_n}');


                if ((locRec.street $[AND] locRec.city) || (locRec.latitude $[AND] locRec.longitude))


                      e.show();


                else


                      e.hide();


          }


         


          //Event.observe(g_form.getControl(${ref}.split('.')[1]), 'change', onChange_cmn_location_show_google_map);


          var l = new GlideEventHandler('onChange_location_show_google_map', onChange_location_show_google_map, '${ref}');


          g_event_handlers.push(l);




          //Pop open google maps window of location specified


          //URL should follow this format...http://maps.google.com/?q=1200 Pennsylvania Ave SE, Washington, District of Columbia, 20003




          function openGoogleMap(reference) {


                var s = reference.split('.');


                var referenceField = reference.substring(s[0].length+1);


                var sysId = g_form.getValue(referenceField);


                //Retrieve the 'Location' record


                var gr = new GlideRecord('cmn_location');


                gr.get(sysId);




                //Create and display the Map URL


                var mapURL = "http://maps.google.com/?q=";


                //Try location and city


                if(gr.street $[AND] gr.city){


                      mapURL = mapURL + gr.street + ',' + gr.city + ',' + gr.state + ',' + gr.zip + ',' + gr.country;


                }


                //Else try latitude and longitude


                else if(gr.latitude $[AND] gr.longitude){


                      mapURL = mapURL + gr.latitude + ',' + gr.longitude;


                }


        //Strip '#' symbols to avoid encoding errors


        mapURL = mapURL.replace(/#/g, "");


                window.open(mapURL);


          }


    </script>


</j:jelly>




Pls mark correct if it is useful and let me know if anything required.



Thanks


Hi Chandresh,



Did you tried creating. Have this worked?


Hi Syed,



I am trying to figure out how to use it in my case. Actually, in my scenario, I need that whenever a ticket generates it will have the postcode details. Also, user will also have the postcode details in their profile. Now, what i want is that if user sets preference in their profile as 10 miles so a back-end script should run to calculate the distance by   ticket and user's postocdes and only those tickets should be visible to the user which is in 10 miles radius.



Your code addresses a similar kind of situation   but I am not able to understand how to use it as I need to automate it and show tickets as per user preferences.