Add current GPS coordinates to frontmatter with Space Script

Well, I found a solution (kind of):

Add a page to the library to get the functions:

-- priority 100

GPSposition = "GPS N/A"

function getGPSposition() 
   if js.window.navigator.geolocation then
     js.window.navigator.geolocation.getCurrentPosition(updateGPSposition)
     return GPSposition.toJSON()
  else
    editor.flashNotification( "Geolocation is not supported by this browser.")
    return "GPS N/A"
  end
end

function updateGPSposition(position)
  GPSposition = position
  GPSposition.readableString = "Location: LAT " + GPSposition.coords.latitude + " / LON " + GPSposition.coords.longitude
  GPSposition.shortstring = GPSposition.coords.latitude + ", " + GPSposition.coords.longitude
  print("DEBUG GPS: " + GPSposition.readableString)
end

event.listen {
  name = "system:ready",
  run = function(e)
    getGPSposition()
  end
}

command.define{
  name = "Insert GPS coordinates",
  run = function()
    getGPSposition()
    editor.flashNotification("My Coords: " + GPSposition)
    editor.insertAtCursor("Location: " + GPSposition.coords.latitude + ", " + GPSposition.coords.longitude, false, true)
  end
}

slashCommand.define {
  name = "GPSposition",
  run = function()
    getGPSposition()
    editor.insertAtCursor("Location: " + GPSposition.coords.latitude + ", " + GPSposition.coords.longitude, false, true)
  end
}

Then you can use the variables in your templates, e.g. GPSposition.shortstring

Only one thing:
I did not find a proper way to wait for the Coords object to get updated. Sometimes I get the old string… But better than nothing :slight_smile: