Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt
nameInfo

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 Block
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;
}