Mobile: Can Obsidian API provide current geo location (GPS) on Android/iOS?

Ah, I got it!

I wrapped it in a function. I suppose for the sake of learning in public, I’ll post what I did so if anyone else that’s new comes along and wants to add locations to their notes, they can.

Follow the instructions to create a script folder in the vault and then add your .js file to that folder, and then call it within your notes.

Here’s the code @AlanG originally wrote above that’s in my location.js file I wrapped in a function:

function location() {
    return Promise.race([
        new Promise((resolve) => {
            fetch('http://ip-api.com/json/')
                .then(response => response.json())
                .then(data => resolve([data.city, data.regionName, data.country].filter(x => !!x).join(', ')))
        }),
        new Promise((resolve) =>
            // If we haven't fetched the location within 300ms,
            // time-out and resolve with the placeholder text instead
            setTimeout(() => resolve('Location'), 300)
        )
    ])
}

module.exports = location;

and then in my notes template, I call it by adding:

<% tp.user.location() %>

If there is a better way, please let me know! However, this seems to be working for me :slight_smile:

Thanks so much for your help!

2 Likes