detecting if location services on mobile devices are enabled

1932
2
Jump to solution
03-07-2018 12:37 PM
FranklinAlexander
Occasional Contributor III

Does anyone know a good way to check to see if location services are enabled/disabled on a mobile device? I have tried using the following with no luck: 

var isMobile = window.matchMedia("only screen and (max-width: 760px)");
if (isMobile.matches) {
   if (!window.navigator.geolocation) {
       this._displayWidgetError(this.nls.noGPSMobileContent);
   }
}
‍‍‍‍‍‍

I am just trying to alert the user with a popup that states that location services must be enabled to use the 'My Location' tool in the widget. I tried locating the above code in the startup() function, as well as the function where the locate button is initiated, and finally in the function that gets called when the button is clicked, but to no avail.. I am just wandering if there is a better method to do this for mobile devices, but haven't found much via Google. 

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Franklin,

  In your widget you can use this global var that WAB set for is mobile.

window.appInfo.isRunInMobile

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Franklin,

  In your widget you can use this global var that WAB set for is mobile.

window.appInfo.isRunInMobile

FranklinAlexander
Occasional Contributor III

Thanks for that suggestion Robert. I couldn't really find a good way to check to see if location services are enabled on a mobile device. It seems that simply using the if statement from above does nothing. Here is what I came up with and it works so-so, but is susceptible to false error messages since it relies on the phones connection and can time out. My reasoned that if I attempted to get the current latitude when the widget loads and it failed, I could assume that the devices location services are not enabled. I think the best method to test for this would be to query the phone settings, but I don't know how you would go about doing that especially since every device is different. 

        if (window.appInfo.isRunInMobile) {
            var content = this.nls.noGPSMobileContent;
            function displayMessages(msg) {
                if (this.widgetErrorNode) {
                  domAttr.set(this.widgetErrorNode, "innerHTML", msg);
                }
                showMessage(msg);
            }

            function showMessage(msg) {
                var alertMessage = new Message({
                    message: msg
                });
                alertMessage.message = msg;
            }

            var latitude = null;
            navigator.geolocation.getCurrentPosition(function (position) {
                latitude = position.coords.latitude;
                console.log("latitude", latitude);
            }, function (error) {
                switch(error.code) {
                    case error.PERMISSION_DENIED:
                        displayMessages("User denied the request for Geolocation.");
                        break;
                    case error.POSITION_UNAVAILABLE:
                        displayMessages("Location information is unavailable.");
                        break;
                    case error.TIMEOUT:
                        displayMessages("The request to get user location timed out. Make sure your location services are enabled");
                        break;
                    case error.UNKNOWN_ERROR:
                        displayMessages("An unknown error occurred.");
                        break;
                }
            }, {
                timeout: 10000,
                enableHighAccuracy: true
            });
         }
0 Kudos