当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Check if user device's GPS is on

问题描述:

I am developing an app using jQuery Mobile with PHP. I am not using Phonegap or other frameworks. I need to find user's geolocation. If user device's GPS is off, then I cant get a location. now I need to find user device's GPS is on or off.

this is what i using now.

function getLocation() {

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(showPosition);

} else {

x.innerHTML = "Geolocation is not supported by this browser.";

}

}

function showPosition(position) {

var lat=position.coords.latitude;

var long=position.coords.longitude;

}

网友答案:

You can call this function on load

      // Function to get location
      function getLocation(){
        navigator.geolocation.getCurrentPosition(function (pos) {
            var lat = pos.coords.latitude;
            var lng = pos.coords.longitude;
            if (lat == null) {
              alert("GPS not activated!");
            }
      else{
      alert("Latitude: "+ lat + " , Longitude: " + lng );
      }
         });
      }
网友答案:

I just solved this one. I am using:

navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {maximumAge: 60000});

In the successCallback i written the codes for what it should do once I got the positions and in the error callback i wrote a simple alert message to prompt the user to turn the GPS on.

分享给朋友:
您可能感兴趣的文章:
随机阅读: