Skip to main content
Skip table of contents

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.

CODE
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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.