2

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;
}
Omar
  • 32,160
  • 9
  • 67
  • 108
JOHN
  • 21
  • 2
  • 3
  • http://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is-enabled also googling it directly i got this one https://developer.appcelerator.com/question/148315/check-to-see-if-gps-is-enabled – goodbytes Nov 25 '14 at 11:50
  • http://stackoverflow.com/questions/6092400/is-there-a-way-to-check-if-geolocation-has-been-declined-with-javascript – A. Wolff Nov 25 '14 at 11:54
  • thank you @bhargav. i think the question and answers belongs to JAVA. PHP is not perfect for this. so i want a javascript. – JOHN Nov 25 '14 at 12:00

4 Answers4

3

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 );
              }
          });
      }
Sl4rtib4rtf4st
  • 420
  • 4
  • 14
RSSHAH
  • 51
  • 4
  • 1
    Even is the GPS is turned off the geolocation api will return a general location based on the location from the mobile towers and possibly from the ip address. – Sl4rtib4rtf4st Dec 04 '18 at 16:01
2

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.

Nair
  • 71
  • 2
  • 14
  • 2
    Even if the device gps is turned off the geoLocation api gives a generic location which might not be very accurate. Please read the question properly – Chetan Kumar Dec 27 '17 at 11:40
0

I Implemented This In Real World Project

KMaps-API GPS.js

<script>


function getLocationw() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPositionw);
  } else { 
    x.innerHTML = "Something Is Wrong";
  }
}

function showPositionw(position) {
    
  lat = position.coords.latitude;
  if(lat != null){
    document.write('<center><div class="alert alert-info" role="alert"> Please Turn On Your GPS </div></center>')
  }  
 
}

getLocationw();

</script>
0

There is no way to check if the device has a GPS module or if it has it enabled through the browser's API. You can, however, use a trick that will likely be good enough for many applications: instead of using the getCurrentPosition(), use the watchPosition() function with an options object { enableHighAccuracy: true } and set a threshold of accuracy that the measurement has to reach for you to accept it as most likely a result based on the GPS module.

What happens when you start to listen to the watchPosition() with enableHighAccuracy set to true is that if GPS module is available, the API will let it know that you're trying to get a measurement and after up to a few seconds the accuracy will go from very high (often thousands of meters - based on IP address, cell tower triangulation, etc.) to a very low (few meters - based on the GPS) and that means that the GPS kicked in. If the accuracy stays at hundreds or thousands of meters, it probably means that there is no GPS module available.

Here's the documentation for the GeolocationCoordinates object (the result within the callback passed to the watchPosition()) which comes with the accuracy field. I wrote a longer post that also contains a code snippet showing how I use the API within React.

m3h0w
  • 1,931
  • 1
  • 21
  • 37