Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
283 changes: 283 additions & 0 deletions extensions/SamuelLouf/Geolocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
// Name: Geolocation
// ID: samuelloufgeolocation
// Description: Get the user's geolocation.
// By: SamuelLouf <https://scratch.mit.edu/users/samuellouf/>
// License: MPL-2.0

(function (Scratch) {
"use strict";

function getGeolocation(
options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }
) {
return new Promise((resolve) => {
function success(pos) {
resolve({
success: true,
latitude: pos.coords.latitude,
longitude: pos.coords.longitude,
accuracy: pos.coords.accuracy,
});
}

function error(err) {
resolve({
success: false,
error: {
code: err.code,
message: err.message,
},
});
}

navigator.geolocation.getCurrentPosition(success, error, options);
});
}

const icon =
"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIxMTAuODAyNSIgaGVpZ2h0PSIxMTAuNDAyNDkiIHZpZXdCb3g9IjAsMCwxMTAuODAyNSwxMTAuNDAyNDkiPjxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0xODQuNTk4NzUsLTEyNC43OTg3NSkiPjxnIGRhdGEtcGFwZXItZGF0YT0ieyZxdW90O2lzUGFpbnRpbmdMYXllciZxdW90Ozp0cnVlfSIgc3Ryb2tlLWxpbmVjYXA9ImJ1dHQiIHN0cm9rZS1saW5lam9pbj0ibWl0ZXIiIHN0cm9rZS1taXRlcmxpbWl0PSIxMCIgc3Ryb2tlLWRhc2hhcnJheT0iIiBzdHJva2UtZGFzaG9mZnNldD0iMCIgc3R5bGU9Im1peC1ibGVuZC1tb2RlOiBub3JtYWwiPj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V2ZW5vZGQiIHN0cm9rZT0iIzAwYTExYiIgc3Ryb2tlLXdpZHRoPSIxLjUiLz48L2c+PC9nPjwvc3ZnPjwhLS1yb3RhdGlvbkNlbnRlcjo1NS40MDEyNTAwMDAwMDAwMDU6NTUuMjAxMjQ1LS0+";

class Geolocation {
constructor() {
this.options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0,
};

this.isWatching = false;
this.watcherID = null;
}

getInfo() {
return {
id: "samuelloufgeolocation",
name: Scratch.translate("Geolocation"),
color1: "#036e15",
color2: "#00A11B",
menuIconURI: icon,
blocks: [
{
opcode: "isSupported",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("is geolocation supported by this device?"),
},
{
opcode: "isAllowed",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("is geolocation allowed?"),
},
"---",
{
opcode: "onUserMove",
blockType: Scratch.BlockType.EVENT,
text: Scratch.translate("when the user's position changes"),
isEdgeActivated: false,
},
{
opcode: "changePositionWatching",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("[VALUE] watching the user's position"),
arguments: {
VALUE: {
type: Scratch.ArgumentType.STRING,
menu: "start_stop",
},
},
},
{
opcode: "isWatchingPos",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate(
"is TurboWarp watching the users position?"
),
},
"---",
{
opcode: "getCurrent",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("get [WHAT]"),
arguments: {
WHAT: {
type: Scratch.ArgumentType.STRING,
menu: "coordinates",
},
},
},
"---",
{
blockType: Scratch.BlockType.LABEL,
text: Scratch.translate("Geolocation Options"),
},
{
opcode: "setTimeoutTo",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("set timeout to [SECONDS] seconds"),
arguments: {
SECONDS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 10,
},
},
},
{
opcode: "addToTimeout",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("add [SECONDS] seconds to timeout"),
arguments: {
SECONDS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1,
},
},
},
{
opcode: "getTimeout",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("get timeout"),
},
{
opcode: "setAccuracy",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("set accuracy to [ACCURACY]"),
arguments: {
ACCURACY: {
type: Scratch.ArgumentType.STRING,
menu: "accuracy",
},
},
},
{
opcode: "isHighAccuracy",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("is the accuracy high?"),
},
],
menus: {
coordinates: {
acceptReporters: true,
items: [
{
text: Scratch.translate("latitude"),
value: "latitude",
},
{
text: Scratch.translate("longitude"),
value: "longitude",
},
{
text: Scratch.translate("accuracy"),
value: "accuracy",
},
],
},
accuracy: {
acceptReporters: true,
items: [
{
text: Scratch.translate("high"),
value: "high",
},
{
text: Scratch.translate("low"),
value: "low",
},
],
},
start_stop: {
acceptReporters: true,
items: [
{
text: Scratch.translate("start"),
value: "start",
},
{
text: Scratch.translate("stop"),
value: "stop",
},
],
},
},
};
}

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

async changePositionWatching(args) {
if (
(args.VALUE == "start" && this.isWatching) ||
(args.VALUE == "stop" && !this.isWatching)
)
return "";
if (!(await this.isAllowed())) return "";

this.isWatching = args.VALUE == "start";

if (args.VALUE == "start") {
this.watcherID = navigator.geolocation.watchPosition(
(pos) => {
var threads = Scratch.vm.runtime.startHats(
"samuelloufgeolocation_onUserMove"
);
for (var thread of threads) {
var coords = pos.toJSON().coords;
coords.success = true;
// @ts-ignore
thread._coordinates = coords;
}
},
() => {},
this.options
);
} else {
navigator.geolocation.clearWatch(this.watcherID);
}
}

isWatchingPos() {
return this.isWatching;
}

async isAllowed() {
if (!this.isSupported()) return false;
// @ts-ignore
return await Scratch.canGeolocate();
}

isSupported() {
return !!navigator.geolocation;
}

setTimeoutTo(args) {
this.options.timeout = Number(args.SECONDS) * 1000;
}

addToTimeout(args) {
this.options.timeout += Number(args.SECONDS) * 1000;
}

getTimeout() {
return this.options.timeout / 1000;
}

setAccuracy(args) {
this.options.enableHighAccuracy = args.ACCURACY === "high";
}

isHighAccuracy() {
return this.options.enableHighAccuracy;
}
}
// @ts-ignore
Scratch.extensions.register(new Geolocation());
// @ts-ignore
})(Scratch);
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"true-fantom/math",
"true-fantom/regexp",
"true-fantom/couplers",
"SamuelLouf/Geolocation",
"DogeisCut/FormatNumbers",
"Lily/AllMenus",
"Lily/HackedBlocks",
Expand Down
1 change: 1 addition & 0 deletions images/SamuelLouf/Geolocation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.