Search code examples
javascriptwindows-runtimewinjs

Is possible to call the GPS settings from a Windows Phone 8.1 App in WinJS?


I have an Windows Phone 8.1 App in WinJS that needs GPS. I´d like that if the GPS is turned off, the app redirect the user to the gps settings of the phone so he can easily turn it on. Is it possible?

Maybe with the use of "Windows.System.Launcher"?


Solution

  • You may try like this :

    // Create a Uri object from a URI string 
                        var uri = new Windows.Foundation.Uri("ms-settings:privacy-location");
                        Windows.System.Launcher.launchUriAsync(uri).then(
                       function (success) {
                       if (success) {
                               // URI launched
                           } else {
                               // URI launch failed
                           }
                       });
    

    I write a sample about how to get the location :

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>WebApp</title>
    
    <!-- WinJS references -->
    <link href="WinJS/css/ui-dark.css" rel="stylesheet" />
    <script src="WinJS/js/base.js"></script>
    <script src="WinJS/js/ui.js"></script>
    
    <!-- WebApp references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
    
    <script type="text/javascript">
            function setText(val, e) {
                document.getElementById(e).value = val;
            }
            function insertText(val, e) {
                document.getElementById(e).value += val;
            }
            var nav = null;
            function requestPosition() {
              if (nav == null) {
                  nav = window.navigator;
              }
              if (nav != null) {
                  var geoloc = nav.geolocation;
                  if (geoloc != null) {
                      geoloc.getCurrentPosition(successCallback, errorCallback);
                  }
                  else {
                      console.log("Geolocation not supported");
                  }
              }
              else {
                  console.log("Navigator not found");
              }
            }
            function successCallback(position)
            {
               setText(position.coords.latitude, "latitude");
               setText(position.coords.longitude, "longitude");
            }
    
            function errorCallback(error) {
                var message = "";
                // Check for known errors
                switch (error.code) {
                    case error.PERMISSION_DENIED:
                        message = "This website does not have permission to use " +
                                  "the Geolocation API";
                        break;
                    case error.POSITION_UNAVAILABLE:
                        message = "The current position could not be determined.";
                        break;
                    case error.PERMISSION_DENIED_TIMEOUT:
                        message = "The current position could not be determined " +
                                  "within the specified timeout period.";
                        break;
                }
                // If it's an unknown error, build a message that includes
                // information that helps identify the situation, so that
                // the error handler can be updated.
                if (message == "")
                {
                    var strErrorCode = error.code.toString();
                    message = "The position could not be determined due to " +
                              "an unknown error (Code: " + strErrorCode + ").";
                }
                console.log(message);
    }
    </script>
    <style></style>
    </head>
    <body>
    
    <label for="latitude">Latitude: </label><input id="latitude" /> <br />
    <label for="longitude">Longitude: </label><input id="longitude" /> <br />
    <input type="button" onclick="requestPosition()" value="Get Latitude and Longitude" />
    
    
    
    </body>
    
    </html>
    

    Here is the document about this