File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Client-Side Components/Client Scripts/Dynamic Location Validation Approach Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ function onSubmit ( ) {
2+ var ga = new GlideAjax ( 'UserLocationUtils' ) ;
3+ ga . addParam ( 'sysparm_name' , 'getUserLocationCoords' ) ;
4+ ga . getXMLAnswer ( function ( response ) {
5+ var locData = JSON . parse ( response ) ;
6+ if ( ! locData ) {
7+ g_form . addErrorMessage ( "No assigned location found for your profile." ) ;
8+ return false ;
9+ }
10+
11+ navigator . geolocation . getCurrentPosition ( function ( position ) {
12+ var userLat = position . coords . latitude ;
13+ var userLng = position . coords . longitude ;
14+ var allowedLat = locData . latitude ;
15+ var allowedLng = locData . longitude ;
16+ var locName = locData . name ;
17+
18+ var R = 6371 ;
19+ var dLat = ( userLat - allowedLat ) * Math . PI / 180 ;
20+ var dLng = ( userLng - allowedLng ) * Math . PI / 180 ;
21+ var a = Math . sin ( dLat / 2 ) ** 2 +
22+ Math . cos ( allowedLat * Math . PI / 180 ) *
23+ Math . cos ( userLat * Math . PI / 180 ) *
24+ Math . sin ( dLng / 2 ) ** 2 ;
25+ var c = 2 * Math . atan2 ( Math . sqrt ( a ) , Math . sqrt ( 1 - a ) ) ;
26+ var distance = R * c ;
27+
28+ if ( distance > 10 ) { // 10 km tolerance
29+ alert ( "You are " + distance . toFixed ( 2 ) + " km away from your registered office: " + locName ) ;
30+ g_form . addErrorMessage ( "Location validation failed." ) ;
31+ return false ;
32+ } else {
33+ g_form . addInfoMessage ( "Location validated successfully within range of " + locName ) ;
34+ return true ;
35+ }
36+ } ) ;
37+ } ) ;
38+ return false ; // Wait for async location validation
39+ }
You can’t perform that action at this time.
0 commit comments