-
Notifications
You must be signed in to change notification settings - Fork 296
Made a Geolocation extension #1943
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
samuellouf
wants to merge
14
commits into
TurboWarp:master
Choose a base branch
from
samuellouf:Geolocation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
32ac69e
Fixed Geolocation extension
samuellouf 9ff402c
Merge branch 'TurboWarp:master' into Geolocation
samuellouf c727d7b
Merge branch 'TurboWarp:master' into Geolocation
samuellouf 2b7df20
Added Translations to Menus
samuellouf 8ce4d67
Merge branch 'TurboWarp:master' into Geolocation
samuellouf ab55fe4
Merge branch 'TurboWarp:master' into Geolocation
samuellouf a7af317
Update Geolocation.js
samuellouf 3d966ba
Merge branch 'TurboWarp:master' into Geolocation
samuellouf 43b396e
Made a few important changes
samuellouf a7f9eef
Merge branch 'master' into Geolocation
samuellouf e09c28a
Formated it
samuellouf f78876e
Merge branch 'Geolocation' of https://github.com/samuellouf/extension…
samuellouf 4511d9b
Made various changes
samuellouf 0bd1ef4
Formated the extension
samuellouf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIxMTAuODAyNSIgaGVpZ2h0PSIxMTAuNDAyNDkiIHZpZXdCb3g9IjAsMCwxMTAuODAyNSwxMTAuNDAyNDkiPjxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0xODQuNTk4NzUsLTEyNC43OTg3NSkiPjxnIGRhdGEtcGFwZXItZGF0YT0ieyZxdW90O2lzUGFpbnRpbmdMYXllciZxdW90Ozp0cnVlfSIgc3Ryb2tlLWxpbmVjYXA9ImJ1dHQiIHN0cm9rZS1saW5lam9pbj0ibWl0ZXIiIHN0cm9rZS1taXRlcmxpbWl0PSIxMCIgc3Ryb2tlLWRhc2hhcnJheT0iIiBzdHJva2UtZGFzaG9mZnNldD0iMCIgc3R5bGU9Im1peC1ibGVuZC1tb2RlOiBub3JtYWwiPjxwYXRoIGQ9Ik0xODQuNTk4NzUsMjM1LjIwMTI1di0xMTAuNDAyNDloMTEwLjgwMjV2MTEwLjQwMjQ5eiIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlP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DEyNTAwMDAwMDAwMDU6NTUuMjAxMjQ1LS0+"; | ||
|
|
||
| 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); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.