Skip to content
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

Geo capacities of Shiny Mobile #208

Open
Leprechault opened this issue Aug 14, 2021 · 1 comment
Open

Geo capacities of Shiny Mobile #208

Leprechault opened this issue Aug 14, 2021 · 1 comment
Labels
question Further information is requested

Comments

@Leprechault
Copy link

Hi Everyone,
I'd like to know if is possible with Shiny Mobile the access of mobile geography coordinates (GPS hardware unit) and extract this information? My final goal is in a given geographic coodinate in a map the Shiny Mobile go to this coordinate of interest.
Thanks in advance.

Alexandre

@dewalex
Copy link

dewalex commented Mar 13, 2023

To obtain the device's GPS location, you can use this javascript.

$(document).ready(function () {

                function getLocation(callback){
                var options = {
                  enableHighAccuracy: true,
                  timeout: 5000,
                  maximumAge: 0
                };
                
	 navigator.geolocation.getCurrentPosition(
					onSuccess, 
					onError,
					options
		);
        
                function onError (err) {
			Shiny.onInputChange("errormess", err.message);
                  Shiny.onInputChange("geolocation", false);
                }
                
                function onSuccess (position) {
                  setTimeout(function () {
                    var coords = position.coords;
                    var timestamp = new Date();
                    
                    console.log(coords.latitude + ", " + coords.longitude, "," + coords.accuracy);
                    Shiny.onInputChange("geolocation", true);
                    Shiny.onInputChange("lat", coords.latitude);
                    Shiny.onInputChange("long", coords.longitude);
                    Shiny.onInputChange("accuracy", coords.accuracy);
			
                    Shiny.onInputChange("time", timestamp)
                  
                    console.log(timestamp);       
                    if (callback) {
                      callback();
                    }
                  }, 1100)
                }
              }
            
              var TIMEOUT = 1000; //SPECIFY
              var started = false;
              function getLocationRepeat(){
                //first time only - no delay needed
                if (!started) {
                  started = true;
                  getLocation(getLocationRepeat);
                  return;
                }
                setTimeout(function () {
                  getLocation(getLocationRepeat);
                }, TIMEOUT);
              };
              getLocationRepeat();
                
            });

Save it as a file (e.g. "myJSfile.js) and put it in your project folder. On your UI, put includeScript("myJSfile.js") and then you will get input$geolocation == TRUE when you have the device's location and you can also call input$lat, input$long, input$accuracy, and input$time. All are updated for the specified interval.

@DivadNojnarg DivadNojnarg added the question Further information is requested label Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants