Add current GPS coordinates to frontmatter with Space Script

I’m still new to SilverBullet and still trying it out, but one of my main use cases for any note app is to create “quick” notes from my phone. In these notes, I like having extra metadata like the gps location of my phone when I created the note.

After some experimenting with the new Space Script functionality, it was suprisingly easy to add what I wanted!

In a new note, I added this space script

silverbullet.registerFunction("getGpsCoords", () => {
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition((position) => {
      const latitude = position.coords.latitude;
      const longitude = position.coords.longitude;
      syscall("editor.flashNotification", `GPS: ${latitude}, ${longitude}`);
      resolve(`${latitude}, ${longitude}`);
    }, (error) => {
      console.log('Geolocation error:', error);
      reject(error);
    });
  });
});

And then in the default Quick Note template (imported from the Core library), I changed the frontmatter to use this new function:

---
description: "Create a quick note"
tags: template
hooks.newPage:
  suggestedName: "Inbox/{{today}} {{time}}"
  confirmName: false
  command: "Quick Note"
  key: "Alt-Shift-n"
frontmatter:
  dateCreated: "{{today}}"
  gps: "{{getGpsCoords}}"
---

Now whenever I create a new note using this template, I automatically get a gps frontmatter key with my current coordinates.

Note: You have to allow your browser to access your location for this to work. Also I noticed sometimes the function gets called twice. I’m not really sure why yet, but it does cause a delay when creating new notes.

11 Likes

This is very interesting, thanks for sharing. I’ll test it as soon as I update my Docker image. Currently is not supporting the space script thingy :smiley:

1 Like

Has anyhow ported this to v2 Lua by any chance?

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:

Maybe this should not be used in production…
I see several issues while testing:

Using it e.g. in chromium, i see an error each time I try to access GPS:
GPS:1 Network location provider at 'https://www.9oo91eapis.qjz9zk/' : ERR_BLOCKED_BY_CLIENT.

Some times this seems to block the whole silverbullet client, it’s getting kind of unresponding…

Will have to dig deeper, but I appreciate every hint :slight_smile: