I have script for geolocalization. It's working only when user is on website. When browser is miminalized or tab is inactive, script is not working. How can I fix it?
var int=self.setInterval(function(){getLocation()},2000);
function getLocation()
{
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log('Position: '+latitude+' '+longitude);
$.ajax({
type : "POST",
data : { latitude : latitude, longitude : longitude },
url : "mylinktosaveposition",
success : function(data){}
});
}
This is a security feature built into browsers, there is no way to get around this! Browsers simply freeze or shut down scripts when they go inactive to increase performance on active tabs and reduce power usage on battery powered devices. I would recommend creating an app if you are looking into geotracing people for whatever reason. Take a look at ionic which is a framework on cordova which uses angular to built apps for ios and android in javascript/html like language.
I am trying to use HTML geolocation to get my position. Funny thing is, it was working brilliantly until some seemingly random point in the day when it just stopped working. Now all I get is the error callback with a message:
Unknown error acquiring position
This happened on the day I first started to develop the app. It is a web app built in Node/Express. The browser I am using is Firefox v53 64-bit.
Location is allowed, and I have also tried a fix that I found online which involves going to about:config and changing geo.wifi.uri from:
https://www.googleapis.com/geolocation/v1/geolocate?key=%GOOGLE_API_KEY%
to
https://www.googleapis.com/geolocation/v1/geolocate?key=test
This did not work for me.
This does however work on my phones Firefox app, but not the Google Chrome app.
Heres an example code snippet:
const geo = navigator.geolocation;
geo.getCurrentPosition(success, failure);
function success(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
$('#coords').val(lat + ',' + lng);
mapView.setCenter(ol.proj.fromLonLat([lng, lat]));
}
function failure(error) {
console.log(error.message);
}
The full page: https://github.com/ThriceGood/Spots/blob/master/views/index.html
Can anyone shed some light on this issue?
What worked for me was changing geo.wifi.uri to:
https://location.services.mozilla.com/v1/geolocate?key=test
As per this page: navigator.geolocation.getCurrentPosition do not work in Firefox 30.0
I'm using Leaflet JS and Cloudmade in my mobile HTML5 application. Unfortunately I can't get the retina support to work.
I use this url to access the cloud made api:
var url = 'http://{s}.tile.cloudmade.com/{key}/{style}#2x/256/{z}/{x}/{y}.png?token={token}';
My key, style and token are replaced by the correct values.
For my leaflet map layer I use the following simple configuration:
L.tileLayer(url, {
detectRetina: true
}).addTo(map);
Unfortunately the result looks really weird.
It seems like something is going wrong with the tiles and the position of them.
If I remove the detectRetina flag I get a correct result in the browser
But as you can see both solutions are not sharp on my retina display (Mac Book Pro).
Has anyone made this working?
Thanks!
This worked for me:
var tileURL = 'http://{s}.tile.cloudmade.com/{api-key}/1714' + (L.Browser.retina? '#2x': '') + '/256/{z}/{x}/{y}.png';
L.tileLayer(tileURL, {detectRetina: true}).addTo(yourMap);
Have you tried with different tile styles and check if that might be the issue?
This code:
navigator.geolocation.getCurrentPosition(
function(position) {
alert(position.coords.latitude, position.coords.longitude);
},
function(error){
alert(error.message);
}, {
enableHighAccuracy: true
,timeout : 5000
}
);
https://jsfiddle.net/FcRpM/ works in Google Chrome at my laptop, but on mobile HTC one S (android 4.1, GPS off, location via mobile networks and wifi enabled), connected to internet via WiFi.
Default browser works fine.
Google Chrome, Opera, Yandex.browser for android fails with "Timeout expired".
other android apps locates me correct.
You can try this. It seems to work on my device (Samsung Galaxy Nexus running Chrome 27.0.1453.90 on Wi-Fi (no data connection, no GPS on))
navigator.geolocation.getCurrentPosition(
function(position) {
alert("Lat: " + position.coords.latitude + "\nLon: " + position.coords.longitude);
},
function(error){
alert(error.message);
}, {
enableHighAccuracy: true
,timeout : 5000
}
);
The problem is that alert only takes strings (in it's original form) however you are passing 2 doubles. Modify the alert box for example to alert('Hey', 'Hello'); and the output will be only Hey. Change the , to + and you'll get the concatenated strings HeyHello. You can't use a + sign inside the alert as the equation will be first executed and then displayed.
Hope this makes it clear.
THERE IS A WORKAROUND: to watchPosition call, and wrapping this in a 5 second wait before clearing the watchID. Code below;
var options = { enableHighAccuracy: true, maximumAge: 100, timeout: 60000 };
if( navigator.geolocation) {
var watchID = navigator.geolocation.watchPosition( gotPos, gotErr, options );
var timeout = setTimeout( function() { navigator.geolocation.clearWatch( watchID ); }, 5000 );
} else {
gotErr();
}
I haven't played around with the "options" values or the timeout delay at the moment, but the above code brings back accurate positioning info on every platform I've tried.
Just finished testing a bunch of mobile devices and the Javascript Geolocation. I used the example code from Google in order to make sure that the problem is not in my own code.
Chrome for Android 4.4 does not seem to work with GPS-only location services and neither does the 2.3 stock browser. They both need "High accuracy" - the use of wireless and 3G/4G networks.
The funny thing is that Firefox for Android works without any problems GPS-only. Only the stock Android browsers (Chrome + Mobile Safari) fail with GPS-only location settings.
And the rest of the gang - Lumia WP8 with GPS, Windows and Linux (both with ADSL) worked perfectly with any location settings.
Well, I ran into this problem yesterday and the issue was that the Geolocation API can only be used over HTTPS. It will work on http://localhost but for other devices, you need to be on a secure connection.
Hope it helps!
After many hours of seeking solution for error3, i only can reboot my phone, and geolocation starts work as usually. So bad...
It was working for me for every simulator but not for android devices
what worked for me was
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
},
(error) => console.log(new Date(), error),
{ enableHighAccuracy: false, timeout: 5000},
);
means instead of three argument I used only two means
{ enableHighAccuracy: false, timeout: 5000}
just add
"geolocation",
"location"
under permissions. Worked for me. :-)
I have the following JavaScript in my page to get the location of the user:
state.currentTimer = navigator.geolocation.watchPosition(success, error, { enableHighAccuracy: true, maximumAge: 5000 });
It is intended that the page is called from the browser on a mobile phone. However the watchPosition method seems incredibly unreliable. Sometimes it won't ever get a location, other times the location is massively off, sometimes it will work fine and then just stop.
I have tried to eliminate the problem of the phones signal by testing it in a city with a good signal.
Is there a better way to get the location from a mobile while in the browser?
I use :
navigator.geolocation.getCurrentPosition(
function(pos) {
pos.coords.latitude;
pos.coords.longitude;
pos.coords.accuracy;
},
function(error) {
error.code;
}
);
All infos are here http://dev.w3.org/geo/api/spec-source.html
Really, that's the only way right now. To ensure that you get more accurate results, you can check the accuracy property of the object passed to your success handler.