Hai guys,

I need to enable the phone gps in order to get accurate geolocation from the web browser... I have this Javascript.. but the script did not enable the gps.. it only obtain using network method.. triangulation ... anyone please help me on this..?

// this is called when the browser has shown support of navigator.geolocation
function GEOprocess(position) {
    // update the page to show we have the lat and long and explain what we do next
 document.getElementById('geo').innerHTML = 'Latitude: ' + position.coords.latitude + ' Longitude: ' + position.coords.longitude;
    // now we send this data to the php script behind the scenes with the GEOajax function
    GEOajax("geo.php?accuracy=" + position.coords.accuracy + "&latlng=" + position.coords.latitude + "," + position.coords.longitude +"&altitude="+position.coords.altitude+"&altitude_accuracy="+position.coords.altitudeAccuracy+"&heading="+position.coords.heading+"&speed="+position.coords.speed+"");
}

// this is used when the visitor bottles it and hits the "Don't Share" option
function GEOdeclined(error) {
  document.getElementById('geo').innerHTML = 'Error: ' + error.message;
}

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(GEOprocess, GEOdeclined);
}else{
  document.getElementById('geo').innerHTML = 'Your browser sucks. Upgrade it.';
}

// this checks if the browser supports XML HTTP Requests and if so which method
if (window.XMLHttpRequest) {
 xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

// this calls the php script with the data we have collected from the geolocation lookup
function GEOajax(url) {
 xmlHttp.open("GET", url, true);nn  b n, 
 xmlHttp.onreadystatechange = updatePage;
 xmlHttp.send(null);
}

// this reads the response from the php script and updates the page with it's output
function updatePage() {
 if (xmlHttp.readyState == 4) {
  var response = xmlHttp.responseText;
  document.getElementById("geo").innerHTML = '' + response;
 }
}

Dani AI

Generated

Short answer: a web page cannot switch the phone’s GPS on. The W3C Geolocation API only requests location from the user agent; the browser/OS decide which provider to use and the user (or a native app) controls system location settings. ’s pointer to the Android docs is relevant for device-side settings, while ’s original getCurrentPosition call will simply receive whatever provider the platform chooses unless a high‑accuracy hint is given. (w3.org)

To increase the chance of a GPS fix, request high accuracy and treat it as a hint (it may be slower and use more power). Example pattern (check permission state, then request a high‑accuracy fix and inspect accuracy in meters):

navigator.permissions.query({ name: 'geolocation' }).then(p => console.log('geo permission', p.state));

const opts = { enableHighAccuracy: true, timeout: 20000, maximumAge: 0 };

navigator.geolocation.getCurrentPosition(function(pos) {
  console.log('lat', pos.coords.latitude, 'lng', pos.coords.longitude, 'accuracy_m', pos.coords.accuracy);
  // accuracy is in meters; <= ~50m often indicates GPS-level precision
}, function(err) {
  console.error('geolocation error', err.code, err.message);
}, opts);

The PositionOptions enableHighAccuracy flag is only a request to the UA; coords.accuracy reports meters and should be used to detect whether the result is GPS‑grade. Use watchPosition() for continuous updates rather than repeated getCurrentPosition() when tracking is needed. (devdoc.net)

Practical checklist (common causes and fixes): serve the page over HTTPS (modern browsers restrict geolocation on insecure origins); verify the browser/site permission (Permissions API or browser UI); on Android check system Location is On and set to High accuracy (and the app/browser has “Precise location” if the OS offers approximate vs precise choices); disable battery‑saving or Wi‑Fi scanning settings that reduce accuracy; test outdoors and allow time for a GPS fix. These platform behaviors and permission changes (Android’s precise/approximate model) are documented in platform guidance. (developer.mozilla.org)

Troubleshooting hints: log position.coords.accuracy to decide if results are network vs GPS; try enableHighAccuracy:true with a longer timeout; test on a device with Location set to High accuracy and with Wi‑Fi scanning enabled; use watchPosition() while moving to get fresh satellite fixes and then clearWatch() when done.

Recommended Answers

All 4 Replies

Usually, a phone's internal settings can be accessed by using it's developer SDK. Which one are you using?

I'm using android...

I still couldn't get the gps ON... ermmm....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.