Skip to content

Conversation

samuellouf
Copy link
Contributor

Hello, folks!
I made a Geolocation extension.
Enjoy!

@github-actions github-actions bot added the pr: new extension Pull requests that add a new extension label Feb 19, 2025
@yuri-kiss

This comment was marked as abuse.

@samuellouf
Copy link
Contributor Author

This is pretty simple, I will give it a review later today if I don't forget

Thanks!

@samuellouf
Copy link
Contributor Author

samuellouf commented Feb 20, 2025

Okay, I'm just gonna add Geocoding Blocks. I'm giving up on that idea. I will probably do a separate "Geocoding" extension.

@samuellouf
Copy link
Contributor Author

Here ! It's finally done!

Copy link
Contributor

@Brackets-Coder Brackets-Coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me! All protocols are being followed and I don't see any reason why this can't be merged now.

"use strict";

function getGeolocation(
options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with geolocation apis but will the timeout affect anything?

This comment was marked as abuse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really : timeout is the limit you give to geolocate the device.
In this case, the getGeolocation function gives the browser 5000ms (or 5s) tops to geolocate the device.
It's the setTimeout function that slows down/stops the code. See on mdn web docs.

Comment on lines 159 to 171
async getCurrent(args) {
if (!(await this.isAllowed())) return "";
var coordinates = await getGeolocation();
if (coordinates.success == true) {
return coordinates[args.WHAT];
} else {
return "";
}
}

async isAllowed() {
return await Scratch.canGeolocate();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuri-kiss are we allowed to make block functions themselves asynchronous or should we put asynchronous functions inside them?

This comment was marked as abuse.

Copy link
Contributor

@PPPDUD PPPDUD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any blocks to actually get my location, which is a pretty necessary feature for a geolocation extension.

@Brackets-Coder
Copy link
Contributor

I can't find any blocks to actually get my location, which is a pretty necessary feature for a geolocation extension.

huh didn't notice that, maybe we're missing something?

@CST1229
Copy link
Collaborator

CST1229 commented Sep 16, 2025

I can't find any blocks to actually get my location, which is a pretty necessary feature for a geolocation extension.

it's an error with the extension. the block has the wrong menu so it points to one that doesn't exist (it should be coordinates)
image

@Brackets-Coder
Copy link
Contributor

I can't find any blocks to actually get my location, which is a pretty necessary feature for a geolocation extension.

it's an error with the extension. the block has the wrong menu so it points to one that doesn't exist (it should be coordinates) image

Thanks, I didn't notice

@samuellouf
Copy link
Contributor Author

I can't find any blocks to actually get my location, which is a pretty necessary feature for a geolocation extension.

it's an error with the extension. the block has the wrong menu so it points to one that doesn't exist (it should be coordinates)

You are right.

@samuellouf
Copy link
Contributor Author

Dunno how I let that slip.

@samuellouf
Copy link
Contributor Author

Fixed it!!

@samuellouf samuellouf requested a review from PPPDUD September 17, 2025 09:34
@Brackets-Coder
Copy link
Contributor

Fixed it!!

I'll check when I can

Copy link
Contributor

@PPPDUD PPPDUD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am quite impressed by this extension. All of the relevant blocks are there, and all checks have passed. @samuellouf You have my blessing.

Copy link
Contributor

@Brackets-Coder Brackets-Coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get longitude and latitude doesn't seem to work for me in Safari (the block says it's supported and it's not an API limitation), and in Firefox longitude is negative where for me it should be positive according to my Maps app.

Once these issues are fixed I think we'll be good to go

 - Changed the default timeout
 - Fixed the `getCurrent` timeout (it wasn't taken into account until this fix)
@samuellouf
Copy link
Contributor Author

I just made a few changes :

  • Changed the default timeout (bc I realised 5 seconds weren't enough)
  • Fixed the getCurrent function (the timeout and the accuracy weren't taken into account until this fix)

@samuellouf
Copy link
Contributor Author

Get longitude and latitude doesn't seem to work for me in Safari (the block says it's supported and it's not an API limitation), and in Firefox longitude is negative where for me it should be positive according to my Maps app.

Perhaps there's something wrong with your computer or your phone. (No offense)
It works for me on both Google Chrome and Firefox on Windows 10, 11, and Linux Mint.

@samuellouf
Copy link
Contributor Author

@PPPDUD, could you check if the position the extension returns is valid?

Copy link
Contributor

@Brackets-Coder Brackets-Coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still doesn't seem to work in Safari

Image

@Brackets-Coder
Copy link
Contributor

Get longitude and latitude doesn't seem to work for me in Safari (the block says it's supported and it's not an API limitation), and in Firefox longitude is negative where for me it should be positive according to my Maps app.

Perhaps there's something wrong with your computer or your phone. (No offense) It works for me on both Google Chrome and Firefox on Windows 10, 11, and Linux Mint.

It's not the computer, I'm on a modern Mac with the latest software version. It works in Firefox, not Safari- it's not a browser limitation because the extension says I support geolocation and Apple's MapJS kit wouldn't work without geolocation capabilities, so something about the get location reporter doesn't work in Safari.

Also, the longitude is still negative, where according to my maps app, it should be positive.

@samuellouf
Copy link
Contributor Author

Get longitude and latitude doesn't seem to work for me in Safari (the block says it's supported and it's not an API limitation), and in Firefox longitude is negative where for me it should be positive according to my Maps app.

Perhaps there's something wrong with your computer or your phone. (No offense) It works for me on both Google Chrome and Firefox on Windows 10, 11, and Linux Mint.

It's not the computer, I'm on a modern Mac with the latest software version. It works in Firefox, not Safari- it's not a browser limitation because the extension says I support geolocation and Apple's MapJS kit wouldn't work without geolocation capabilities, so something about the get location reporter doesn't work in Safari.

That's interesting. I don't get why it isn't working. It should work.
Is the longitude still negative?

@samuellouf
Copy link
Contributor Author

Should I add a "when user moves" block? (It would be a hat, ofc)
It would be paired with latitude, longitude, accuracy blocks.
It seems to be easily doable.

@Brackets-Coder
Copy link
Contributor

Get longitude and latitude doesn't seem to work for me in Safari (the block says it's supported and it's not an API limitation), and in Firefox longitude is negative where for me it should be positive according to my Maps app.

Perhaps there's something wrong with your computer or your phone. (No offense) It works for me on both Google Chrome and Firefox on Windows 10, 11, and Linux Mint.

It's not the computer, I'm on a modern Mac with the latest software version. It works in Firefox, not Safari- it's not a browser limitation because the extension says I support geolocation and Apple's MapJS kit wouldn't work without geolocation capabilities, so something about the get location reporter doesn't work in Safari.

That's interesting. I don't get why it isn't working. It should work. Is the longitude still negative?

Yes, but I'm not 180º on the other side of the world. A theory, I updated to the latest major macOS Tahoe release recently and it's a .0 version. I've discovered a few UI inconsistencies after the redesign so there may be potential functionality issues in the latest version, but it's very unlikely.

@Brackets-Coder
Copy link
Contributor

Should I add a "when user moves" block? (It would be a hat, ofc) It would be paired with latitude, longitude, accuracy blocks. It seems to be easily doable.

Depends on whether users would use it and how performant it would be

@Brackets-Coder
Copy link
Contributor

Hmm... it seems like I have location services disabled in settings and Firefox is attempting to override it, so it may be faulty. I'll test it later when I have time

@PPPDUD PPPDUD self-requested a review September 19, 2025 14:10
@PPPDUD
Copy link
Contributor

PPPDUD commented Sep 19, 2025

Should I add a "when user moves" block? (It would be a hat, ofc) It would be paired with latitude, longitude, accuracy blocks. It seems to be easily doable.

@samuellouf That is genius, please add that! You might want to add a way to enable or disable that block though, since it's probably gonna eat up time pretty easily (see @Brackets-Coder's comment).

@samuellouf
Copy link
Contributor Author

Should I add a "when user moves" block? (It would be a hat, ofc) It would be paired with latitude, longitude, accuracy blocks. It seems to be easily doable.

@samuellouf That is genius, please add that! You might want to add a way to enable or disable that block though, since it's probably gonna eat up time pretty easily (see @Brackets-Coder's comment).

Done !

I added some code that sends the event, gets the threads to which the event was sent, and returns the value in the get [WHAT] block.

    async getCurrent(args, util) {
      if (!(await this.isAllowed())) return "";
      var coordinates =
        util.thread._coordinates || (await getGeolocation(this.options));
        // read the coordinates in the thread if present OR get the current coordinates
      if (coordinates.success == true) {
        return coordinates[args.WHAT];
      } else {
        return "";
      }
    }

    async changePositionWatching(args) {
...
        this.watcherID = navigator.geolocation.watchPosition(
          (pos) => {
            var threads = Scratch.vm.runtime.startHats(
              "samuelloufgeolocation_onUserMove"
            ); // Trigger the hat
            for (var thread of threads) { // for each thread
              var coords = pos.toJSON().coords; // Turn the coordinates into JSON
              coords.success = true; // Add success
              // @ts-ignore
              thread._coordinates = coords; // Add the coordinates to the thread
            }
          },
          () => {},
          this.options
        );
...
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: new extension Pull requests that add a new extension
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants