logo

NJP

To get latitude, longitude,timezone and UTC offset on the basis of location address

Import · Feb 18, 2019 · article

There is a proposed solution to fetch the details of a location record suce as latitude,longitude,timezone,offset etc based on the address fields such as street,city,country in servicenow.

This requires the Google API which needs a google key to genenerate this.So,please follow this link to generate the google key.

https://developers.google.com/maps/documentation/javascript/get-api-key

Now after fetching the key,perform these steps.Please note I have created two new field u_timezone and u_offset on cmn_location location table for capturing these details.

Table:Location

Business rule type:before

when to run:insert/update

1. To update latitude and longitude

condition:current.street.changes() || current.city.changes() || current.state.changes() || current.zip.changes() || current.country.changes()

script:

(function executeRule(current, previous /*null when async*/) {

if (current != null) { var street = new String(current.street);street = street.replaceAll("\r","");street = street.replaceAll("\n"," ");

street = street.replaceAll(" ","+");

var city = new String(current.city).replaceAll(" ","+");var state = new String(current.state).replaceAll(" ","+");

var zip = new String(current.zip).replaceAll(" ","+");

var address = "address=" + street + "," + city + "," + state + "," + zip + "&key='GOOGLE_API_KEY";

var ws = new HTTPAdaptor("https://maps.googleapis.com/maps/api/geocode/json?");

var ret = ws.doGet(address);

var lat = 0,

lng = 0;

try {

var output = new JSONParser().parse(ret);

if (output.status == "OK") {

lat = output['results'][0]['geometry']['location']['lat']; //process the o/p to fetch latitude

lng = output['results'][0]['geometry']['location']['lng']; //process the o/p to fetch longitude

current.latitude = lat; //set the latitude current.longitude = lng; //set the longitude

}

else {

gs.logErr("Get_Lat_Long Lookup Error: " + output.status);

gs.logErr("Address: " + address); gs.logErr("API Response: " + ret);

}

} catch (err) { gs.logErr("Geocoding error: " + jsonOutput); }

}

})(current, previous);

2. To update timezone and offset

condition:current.latitude.changes() || current.longitude.changes()

script:

rawOffset

(function executeRule(current, previous /*null when async*/) {

if (current != null) {

var lat = new String(current.latitude).replaceAll(" ","+");

var lon = new String(current.longitude).replaceAll(" ","+");

var address = "location=" + lat +','+lon +"&timestamp=1478880000&key='Google_API_KEY'";

var ws = new HTTPAdaptor("https://maps.googleapis.com/maps/api/timezone/json?");

var ret = ws.doGet(address);

var tz = 0;

try {

var output = new JSONParser().parse(ret);

if (output.status == "OK") {

current.u_timezone = output.timeZoneId; //set the timezone

current.u_offset = output.rawOffset; //set the offset

} else {

gs.logErr("Get_Lat_Long Lookup Error: " + output.status);

gs.logErr("Address: " + address);

gs.logErr("API Response: " + ret);

}

} catch (err) {

gs.logErr("Geocoding error: " + jsonOutput);

}}

})(current, previous);

Regards,

Munender

image

View original source

https://www.servicenow.com/community/itsm-articles/to-get-latitude-longitude-timezone-and-utc-offset-on-the-basis/ta-p/2301859