Skip to content

New Extension: Location #2179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
108 changes: 108 additions & 0 deletions extensions/wiktorlaskowski/location.js
Original file line number Diff line number Diff line change
@@ -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 {

Check failure on line 5 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

All extension code must be within (function (Scratch) { ... })(Scratch);
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',

Check failure on line 18 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

Extension name should usually be translated
color1: '#003366',
color2: '#002244',
color3: '#001122',
blocks: [
{
opcode: 'getLatitude',
blockType: Scratch.BlockType.REPORTER,
text: 'latitude',

Check failure on line 26 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

Block text should usually be translated
},
{
opcode: 'getLongitude',
blockType: Scratch.BlockType.REPORTER,
text: 'longitude',

Check failure on line 31 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

Block text should usually be translated
},
{
opcode: 'getAccuracy',
blockType: Scratch.BlockType.REPORTER,
text: 'location accuracy',

Check failure on line 36 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

Block text should usually be translated
},
{
opcode: 'setFallbackLocation',
blockType: Scratch.BlockType.COMMAND,
text: 'set fallback location latitude [LAT] longitude [LON]',

Check failure on line 41 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

Block text should usually be translated
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() {

Check failure on line 59 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

Async method '_getUpdatedLocation' has no 'await' expression
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());

Check failure on line 108 in extensions/wiktorlaskowski/location.js

View workflow job for this annotation

GitHub Actions / lint

All extension code must be within (function (Scratch) { ... })(Scratch);
3 changes: 3 additions & 0 deletions images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
1 change: 1 addition & 0 deletions images/wiktorlaskowski/location.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading