Firefox 43 seems to have broken W3C (navigator.geolocation) location services - javascript

I've just upgraded to Firefox 43 and the HTML5/W3C location service on my site no longer works.
I've checked the release notes for 43 and can find no reference to any changes.
This is the JS that I'm using to get the current Lat/Long location:
// Try W3C Geolocation (Preferred)
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) { // start of callback function for success
alert("ok");
},
function () { // start of callback function for getPos failure
alert("Unable to determine your current location");
}); // end of get position call
} else { // Browser doesn't support Geolocation
alert("Geolocation not available");
}
and here is a JSFiddle showing that in Firefox 43 I get the 'unable to determine' error, rather than geolocation not available...
Note, geolocation still seems to be working in Chrome with the above, and Firefox 43 on Android is still working without problems.

A bit strange...
With the download of the v43.0.1 update this problem resolved itself before the update was applied!
There is no mention in the 43.0.1 release notes of any change that would affect geolocation either, but the problem has definitely resolved.

Related

How to always prompt for geolocation in browser once blocked? [duplicate]

im building an app through phonegap, with a geolocation button.
if a user denies permission for geolocation the first time, how can i ask for permission again when they click the geolocation button again?
my code structure at the moment is:
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, positionError);
} else {
hideLoadingDiv()
showError('Geolocation is not supported by this device')
}
}
function positionError() {
hideLoadingDiv()
showError('Geolocation is not enabled. Please enable to use this feature')
}
You can't.
The only thing you can do is to display the instructions to reactivate the location sharing in his browser's settings (https://support.google.com/chrome/answer/142065?hl=en).
Two ways of doing this:
If you have a version of Chrome bigger than 83.0.4103.97 then use the lock icon in the URL
For older versions of Chrome the bellow code will work fine:
The bellow code only works on Chrome.
Steps:
Open Chrome
Open the console
Copy in the console
var allowGeoRecall = true;
var countLocationAttempts = 0;
Copy in the console the functions
function getLocation() {
console.log('getLocation was called')
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,
positionError);
} else {
hideLoadingDiv()
console.log('Geolocation is not supported by this device')
}
}
function positionError() {
console.log('Geolocation is not enabled. Please enable to use this feature')
if(allowGeoRecall && countLocationAttempts < 5) {
countLocationAttempts += 1;
getLocation();
}
}
function showPosition(){
console.log('posititon accepted')
allowGeoRecall = false;
}
Run the function in the console
getLocation();
After running this you will be asked to allow to share your position. If your response is negative you will be asked again until you agree.
HINT: If your user has a negative response, let him know why you need the coordinates. Is vital for him to understand that this step is vital for the good run of the web app.
This can be reset in Page Info which can be accessed by clicking the lock icon next to the URL and allowing Location

navigator.geolocation.getCurrentPosition/watchPosition is not working in android 6.0

Here is my javascript code :
function getLocation() {
//navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:30000, enableHighAccuracy:true});
var mobile =jQuery.browser.mobile;
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if(mobile){
watchLocation(function(coords) {
var latlon = coords.latitude + ',' + coords.longitude;
//some stuff
}, function() {
alert("error");
});
} else {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("error");
}
}
}
function watchLocation(successCallback, errorCallback) {
successCallback = successCallback || function(){};
errorCallback = errorCallback || function(){};
// Try HTML5-spec geolocation.
var geolocation = navigator.geolocation;
if (geolocation) {
// We have a real geolocation service.
try {
function handleSuccess(position) {
alert("position:"+position.coords);
successCallback(position.coords);
}
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000 // 5 sec.
});
} catch (err) {
errorCallback();
}
} else {
errorCallback();
}
}
I have tried both getCurrentPosition and watchPosition.
It's reaching errorCalback() method when control comes to geolocation.watchPosition line.
I am testing in Motorola G 2nd Gen with Android 6 and Google chrome browser and opera mini.
Update 1: When I put alert in error call back function I got error:1; message:Only Secure origins are allowed(see:link).
navigator.geolocation.getCurrentPosition(showPosition, function(e)
{ alert(e); //alerts error:1; message:Only Secure origins are allowed(see: )
console.error(e);
})
Update 2: With the help from g4s8 I am able to findout that the error is because of insecure URL. i.e only accessing with http instead of https.But then also I bypassed that in browser by clicking advanced button.But it will prompt for Do you want to allow location, which I don't want..is there any way to access location without prompting it?
Your page should be served over https to access geolocation API.
See Geolocation API Removed from Unsecured Origins
Starting with Chrome 50, Chrome no longer supports obtaining the user's location using the HTML5 Geolocation API from pages delivered by non-secure connections
...
It is an important issue as it will directly impact any site that requires use of the geolocation API and is not served over https
To fix this serve your page over https or on localhost.
Thank you...Is there any way to bypass it??
You can try to use some geolocation services, e.g.
geoip2, Geolocation request
how to use them? can you show an example?? from those two can i access user location without knowing them?
GeoIP2 detect you location by ip address. You can obtain country (geoip2.country()) and city (geoip2.city) with js lib:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
Here https://dev.maxmind.com/geoip/geoip2/javascript/ you can find full documentation.
Google maps geolocation is google service, so you need to get api key first. Then you can send POST request with json parameters to https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY and get the response:
{
"location": {
"lat": 51.0,
"lng": -0.1
},
"accuracy": 1200.4
}
where location is the user’s estimated latitude and longitude, in degrees,
and accuracy is the accuracy of the estimated location, in meters.
Full json parameters defenition you can find in "Request body" section here https://developers.google.com/maps/documentation/geolocation/intro#overview
Also you can find useful those answers: getCurrentPosition() and watchPosition() are deprecated on insecure origins
using IP it provides only country and city..??
Yes, only this.
will it provide physical location like how getCurrent Position provides??
No, you can't get physical location, because it can be accessed only via gelocation API, that was restricted in insecure context.
Also you have one more option. You can host only one page (that access geolocation API) on https server, and redirect from this page to your http site with user location in get parameters.
/* https page */
navigator.geolocation.getCurrentPosition(function (result) {
window.location.href = "http://your.site.com/http-page?lat=" + result.latitude + "&long=" + result.longitude;
});

Javascript: running on mobile but not on desktop(chrome, opera, safari)

I'm working with the HTML5 geolocation api. I'm getting a weird error. i got a function called doGeolocate that is called when the user land on the page. That load the map with the wright location data. And i got a button that recall the same function when ever the user want to recenter the map. But when a user tap the button on a desktop(chrome,opera, safari) except firefox it doesn't work.
While on the other there is no problem. Once debug it seem like the code locationCallback isn't call properly the second time even tho it is on load.
doGeolocate: function(){
if (navigator.geolocation) {
console.log("doGeolocate");
var locationCallback = helper.callback(this, function(position) {
this.initialGeolocationPosition(position);
if(!this.positionMarker){
this.createPositionMarker(position);
}
});
var err = function(err) {
console.log("");
};
var options = {
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(locationCallback,err,options);
}
},

navigator.geolocation.getCurrentPosition doesn't work on android google chrome

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. :-)

Geolocation hangs after x attemps, works after browser restart

I'm working on a script that needs to grab my geolocation. It works most of the time, However every now and then the geolocation is not aquired and I have to restart the browser for it to work again. This occurs in Safari and FF (Not tested in IE and Chrome). Does anybody know what could be causing this? I'm using this bit of code from the book "HTML 5".
<script type="text/javascript">
function loadDemo() {
if(navigator.geolocation) {
document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";
navigator.geolocation.getCurrentPosition(updateLocation);
}
}
function updateLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
if (!latitude || !longitude) {
document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser, but location is currently not available.";
return;
}
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;
}
FF sometimes hangs, don't know about safari. It doesnt work in IE yet as far as i know. It seems to work great in Chrome so far.
You can "solve" this by setting a timeout. It still doesn't work but at least the script is terminated after a while. I use this code.
If you you found a good fix please let me know.
navigator.geolocation.getCurrentPosition(
function(position) {
//succes handler
},
function errorCallback(error) {
//error handler
},
{
enableHighAccuracy:false,
maximumAge:Infinity,
timeout:5000
}
);

Categories