Get GeoLocation In The Browser - javascript

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.

Related

Edge extension crashes the whole browser on API call after latest update (launchWebAuthFlow)

EDIT: THIS HAS JUST BEEN FIXED IN THE 98.0.1108.43 UPDATE!
EDIT2: NEVERMIND! THE LOGIN POPUP WINDOW NOW SHOWS, BUT THE MOMENT YOU TRY TO SIGN IN IT STILL CRASHES!
EDIT3: FINALLY FIXED :)
I'm creating a browser extension for my work.
The function launchWebAuthFlow has worked wonderfully since I started this project, but then Edge decided to update (to 96.0.1054.43, and now again to 96.0.1054.53, and now again to 96.0.1054.57, and now again to 96.0.1054.62, now again to 97.0.1072.55, and now again to 97.0.1072.62 after MS sent me an email saying they fixed it, and now again to 97.0.1072.69, and now again to 97.0.1072.76) and now the whole browser instantly crashes when the function launchWebAuthFlow is called. I have tried downgrading Edge, but I haven't found any download links to previous versions.
My authUrl looks like this (and has worked this far before the updates!):
https://login.microsoftonline.com/common/oauth2/authorize
I've stripped out the arguments for privacy.
This code alone doesn't crash, but also doesn't show any window:
chrome.identity.launchWebAuthFlow(
{
interactive: true,
url: authUrl
}
);
This code crashes (!) and it should show a window (and this is the code I've been using which has worked for a long time until the update):
chrome.identity.launchWebAuthFlow(
{
interactive: true,
url: authUrl
},
(response) =>
{
console.log("This never prints, because the browser crashes!");
}
);
I found this: Chrome Extension crashes the Microsoft Edge browser while calling launchWebAuthFlow API
The accepted answer for that question says this is a workaround:
return await global.chrome.windows.create({
focused: true,
height : 800,
width: 800,
url: authURL
})
But it doesn't work for me. chrome.windows.create creates and shows a window but returns undefined, so I don't know what to do.

navigator.geolocation.getcurrentposition is not working in safari

This is the code i am using for getting locations. But unfortunately it prints "failure call back " always.
var location_timeout = setTimeout(function(){alert('failed');}, 10000);
navigator.geolocation.getCurrentPosition(function(position) {
findInStore.isGeoLocationPos = position;
clearTimeout(location_timeout);
alert('Succ call back')
findInStore.onDirectionLocation(event);
},function(){clearTimeout(location_timeout);
alert('failure call back');});
Try
http://stackoverflow.com/questions/7350033/geolocation-not-working-in-safari
http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt
and few more. But none helps. getCurrentPosition call fails. Any suggestions? I am not good at this.
Turn Wifi on on your computer and it will work. Safari use wifi to geolocalize your computer :-)

HTML 5 Issues with showing google map

I have been looking at the google maps html5/javascript code and am attempting to make a app which finds someone current location through longitude and latitude then geo reverses it to give then their address. However when i run the code it just gives a blank screen. I am really new to html5 and if anyone could point to me what i am doing wrong i would be really grateful.
the error is geocoder = new google.maps.Geocoder(): must be geocoder = new google.maps.Geocoder();
By default, navigator.geolocation.getCurrentPosition() has a timeout value of infinite. In the getCurrentPosition's options parameter, specify a timeout value and I think you will find that is where your error lies.
... }, function() {
handleNoGeolocation(true);
}, { timeout: 4000 });

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

HTML5 geolocation - error callback doesn't always fire

I'm using the Geolocation API in this fairly simple way:
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
console.log("gettingPosition: " + position);
myFunction(position(;
}, function(error){
console.log('did not get position');
console.log(error);
});
}
In some cases (Firefox 12.0 is the browser I have in front of me) no callback fires if the user clicks "Not Now", not even the error. Is this expected? If so, is my only option to follow the technique in Geolocation feedback while accepting the request and set my own timeout?
This appears to be by design (for Firefox). See Josh's comments in https://bugzilla.mozilla.org/show_bug.cgi?id=635175
The logic is that clicking "not now" is a way for the user to dismiss the permission dialog without committing to a decision about whether or not to allow geolocation permissions.
It looks like FF16 and Chrome-Latest both operate this way.
(P.S. Don't shoot the messenger. :p)

Categories