shinyMobile icon indicating copy to clipboard operation
shinyMobile copied to clipboard

Geo capacities of Shiny Mobile

Open Leprechault opened this issue 3 years ago • 1 comments

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

Leprechault avatar Aug 14 '21 15:08 Leprechault

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.

dewalex avatar Mar 13 '23 19:03 dewalex