Geolocation permission for mobile app

Yes for sure, that’s the way I do it in my vault. Use the original code with return, not the one with tR

Thanks so much for your help, but I’m still stuck :frowning:

I took your original code above (with return) and created a .js file and placed the .js file (named location.js) in a scripts folder and made sure templater saw it in setting. Then and when I call it in my daily notes template

 <% tp.user.location() %>

it continues to say there is a parsing issue and it doesn’t run.

What could I be doing wrong?

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!

4 Likes

This script works perfectly on my computer but I keep getting a parsing error on mobile. Has anyone found a way to get around this?

1 Like

In v1.11, on mobile, plugin developers can now request the user location.

For desktop, create a separate thread in Developers: Plugin & API. Thanks.

3 Likes