最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if user device's GPS is on - Stack Overflow

programmeradmin6浏览0评论

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;
}

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;
}
Share Improve this question edited Nov 25, 2014 at 11:51 Omar 31.7k9 gold badges72 silver badges116 bronze badges asked Nov 25, 2014 at 11:48 JOHNJOHN 511 gold badge2 silver badges3 bronze badges 3
  • stackoverflow./questions/843675/… also googling it directly i got this one developer.appcelerator./question/148315/… – goodbytes Commented Nov 25, 2014 at 11:50
  • stackoverflow./questions/6092400/… – A. Wolff Commented Nov 25, 2014 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 Commented Nov 25, 2014 at 12:00
Add a ment  | 

4 Answers 4

Reset to default 5

There is no way to check if the device has a GPS module or if it has it enabled through the browser's API. Your laptop will, for example, try to estimate the position based on the IP address with very poor accuracy.

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 distance 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 es with the accuracy field. I wrote a longer post that also contains a code snippet showing how I use the API within React.

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.

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)

  1. 暂无评论