Geolocation

Collection of latitude and longitude from the client’s browser is a common theme in modern applications. While the user must allow location identification in their browser, when enabled, it can be helpful to track where users are. The example tracks the latitude and longitude of each save operation to a multi-value field.

The following script uses the browser’s native geolocation api to pull the users coordinates and writes them to a multi-value field named Geo Location.

var viewHash = 'locationAware-'+$$inject.properties.document.fileId; if(!sessionStorage.getItem(viewHash)) { sessionStorage.setItem(viewHash, true) if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(gatherPosition); return "Position set"; } return "Position could not be set."; } function gatherPosition(position){ let geoData = "Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude; let geoArray = $$inject.fields['Geo Location']; geoArray.push(geoData); $$inject.fields['Geo Location'] = geoArray; }

When a document with the Live Field above is opened, the location data is gathered and appended to the Geo Location field, which is defined as multi-value. The new data is appended to the existing multi-value data set, preserving the overall history of the field.