From 797dcb718b03221ab666123bccfcebe72741138a Mon Sep 17 00:00:00 2001 From: Wiktor Laskowski <117634264+wiktorlaskowski@users.noreply.github.com> Date: Mon, 23 Jun 2025 17:01:05 +0200 Subject: [PATCH 1/3] Create location.svg --- images/wiktorlaskowski/location.svg | 1 + 1 file changed, 1 insertion(+) create mode 100644 images/wiktorlaskowski/location.svg diff --git a/images/wiktorlaskowski/location.svg b/images/wiktorlaskowski/location.svg new file mode 100644 index 0000000000..0e42d0367e --- /dev/null +++ b/images/wiktorlaskowski/location.svg @@ -0,0 +1 @@ + From 5a14c733ca832fe71068c39c69cc6207450d48ec Mon Sep 17 00:00:00 2001 From: Wiktor Laskowski <117634264+wiktorlaskowski@users.noreply.github.com> Date: Mon, 23 Jun 2025 17:07:40 +0200 Subject: [PATCH 2/3] Create location.js --- extensions/wiktorlaskowski/location.js | 108 +++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 extensions/wiktorlaskowski/location.js diff --git a/extensions/wiktorlaskowski/location.js b/extensions/wiktorlaskowski/location.js new file mode 100644 index 0000000000..caede29dfe --- /dev/null +++ b/extensions/wiktorlaskowski/location.js @@ -0,0 +1,108 @@ +// Name: Location +// ID: location +// Description: Allows getting the location's latitude, longitude, and accuracy. Supports fallback locations. +// By: Wiktor Laskowski +class LocationExtension { + constructor(runtime) { + this.runtime = runtime; + this.latitude = 51.5074; // Default: London + this.longitude = -0.1272; + this.accuracy = 0; + this.fallbackLatitude = 51.5074; + this.fallbackLongitude = -0.1272; + } + + getInfo() { + return { + id: 'location', + name: 'Location', + color1: '#003366', + color2: '#002244', + color3: '#001122', + blocks: [ + { + opcode: 'getLatitude', + blockType: Scratch.BlockType.REPORTER, + text: 'latitude', + }, + { + opcode: 'getLongitude', + blockType: Scratch.BlockType.REPORTER, + text: 'longitude', + }, + { + opcode: 'getAccuracy', + blockType: Scratch.BlockType.REPORTER, + text: 'location accuracy', + }, + { + opcode: 'setFallbackLocation', + blockType: Scratch.BlockType.COMMAND, + text: 'set fallback location latitude [LAT] longitude [LON]', + arguments: { + LAT: { + type: Scratch.ArgumentType.NUMBER, + defaultValue: 51.5074 + }, + LON: { + type: Scratch.ArgumentType.NUMBER, + defaultValue: -0.1272 + } + } + } + ], + menus: {} + }; + } + + // Return a promise that resolves when the location is updated + async _getUpdatedLocation() { + return new Promise((resolve) => { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition( + (position) => { + this.latitude = position.coords.latitude; + this.longitude = position.coords.longitude; + this.accuracy = position.coords.accuracy; + resolve(); + }, + (error) => { + console.warn("Geolocation error, using fallback:", error.message); + this.latitude = this.fallbackLatitude; + this.longitude = this.fallbackLongitude; + this.accuracy = 0; + resolve(); + } + ); + } else { + console.warn("Geolocation not supported, using fallback."); + this.latitude = this.fallbackLatitude; + this.longitude = this.fallbackLongitude; + this.accuracy = 0; + resolve(); + } + }); + } + + async getLatitude() { + await this._getUpdatedLocation(); + return this.latitude; + } + + async getLongitude() { + await this._getUpdatedLocation(); + return this.longitude; + } + + async getAccuracy() { + await this._getUpdatedLocation(); + return this.accuracy; + } + + setFallbackLocation(args) { + this.fallbackLatitude = args.LAT; + this.fallbackLongitude = args.LON; + } +} + +Scratch.extensions.register(new LocationExtension()); From 95456e0750f2bd5328f78399940e8b6075948c99 Mon Sep 17 00:00:00 2001 From: Wiktor Laskowski <117634264+wiktorlaskowski@users.noreply.github.com> Date: Mon, 23 Jun 2025 17:14:11 +0200 Subject: [PATCH 3/3] Update credits --- images/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/images/README.md b/images/README.md index 55105c2c9f..c796d93c77 100644 --- a/images/README.md +++ b/images/README.md @@ -321,3 +321,6 @@ All images in this folder are licensed under the [GNU General Public License ver ## CubesterYT/KeySimulation.svg - Created by [@SharkPool-SP](https://github.com/SharkPool-SP/) + +## wiktorlaskowski/location.svg + - Created by [@wiktorlaskowski](https://github.com/wiktorlaskowski/)