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

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

Related

How to set playback rate from chrome browser?

This one if probably for chromecast API devs.
Android and iOS have a setPlaybackRate method, but the Chrome Sender API doesn't appear to have an equivalent feature.
Is there a javascript method to do this besides using sendMessage?
If not, please consider this a feature request! :D
I know you've long since moved on from this problem, but here is what got me rolling on this, and yours was the only question I found about it.
playerTarget.setHalfSpeed = function (){
var media = castSession.getMediaSession();
castSession.sendMessage("urn:x-cast:com.google.cast.media",{
type: "SET_PLAYBACK_RATE",
playbackRate: 0.5,
mediaSessionId: media.mediaSessionId,
requestId: 2
}).then(
function (a) { console.log('Set playback rate success'); },
function (errorCode) {
console.log('Set playback rate error: ' + errorCode);
});
}.bind(this);

html geolocation: Unknown error acquiring position

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

node-serialport on windows with multiple devices hangs

I've been experimenting with node-serialport library to access devices connected to a USB hub and send/receive data to these devices. The code works fine on linux but on windows(windows 8.1 and windows 7) I get some odd behaviour. It doesn't seem to work for more than 2 devices, it just hangs when writing to the port. The callback for write method never gets called. I'm not sure how to go about debugging this issue. I'm not a windows person, if someone can give me some directions it would be great.
Below is the code I'm currently using to test.
/*
Sample code to debug node-serialport library on windows
*/
//var SerialPort = require("./build/Debug/serialport");
var s = require("./serialport-logger");
var parsers = require('./parsers');
var ee = require('events');
s.list(function(err, ports) {
console.log("Number of ports available: " + ports.length);
ports.forEach(function(port) {
var cName = port.comName,
sp;
//console.log(cName);
sp = new s.SerialPort(cName, {
parser: s.parsers.readline("\r\n")
}, false);
// sp.once('data', function(data) {
// if (data) {
// console.log("Retrieved data " + data);
// //console.log(data);
// }
// });
//console.log("Is port open " + sp.isOpen());
if(!sp.isOpen()) {
sp.open(function(err) {
if(err) {
console.log("Port cannot be opened manually");
} else {
console.log("Port is open " + cName);
sp.write("LED=2\r\n", function(err) {
if (err) {
console.log("Cannot write to port");
console.error(err);
} else {
console.log("Written to port " + cName);
}
});
}
});
}
//sp.close();
});
});
I'm sure you'd have noticed I'm not require'ing serialport library instead I'm using serialport-logger library it's just a way to use the serialport addons which are compiled with debug switch on windows box.
TLDR; For me it works by increasing the threadpool size for libuv.
$ UV_THREADPOOL_SIZE=20 && node server.js
I was fine with opening/closing port for each command for a while but a feature request I'm working on now needs to keep the port open and reuse the connection to run the commands. So I had to find an answer for this issue.
The number of devices I could support by opening a connection and holding on to it is 3. The issue happens to be the default threadpool size of 4. I already have another background worker occupying 1 thread so I have only 3 threads left. The EIO_WatchPort function in node-serialport runs as a background worker which results in blocking a thread. So when I use more than 3 devices the "open" method call is waiting in the queue to be pushed to the background worker but since they are all busy it blocks node. Then any subsequent requests cannot be handled by node. Finally increasing the thread pool size did the trick, it's working fine now. It might help someone. Also this thread definitely helped me.
As opensourcegeek pointed all u need to do is to set UV_THREADPOOL_SIZE variable above default 4 threads.
I had problems at my project with node.js and modbus-rtu or modbus-serial library when I tried to query more tan 3 RS-485 devices on USB ports. 3 devices, no problem, 4th or more and permanent timeouts. Those devices responded in 600 ms interval each, but when pool was busy they never get response back.
So on Windows simply put in your node.js environment command line:
set UV_THREADPOOL_SIZE=8
or whatever u like till 128. I had 6 USB ports queried so I used 8.

webkit geolocation with high accuracy using only the GPS radio

Hey guys I'm running into a situation where I'm needing to get forms autopopulated with location data and when the phone or tablet is using ONLY the GPS for the location data, I can't get any sort of anything out of it. When using "wireless networks" it works on my 2.4 phone, 3.2 tab, and 4.0 tab. Without those others enabled, I'm up the creek and too weak in my android knowledge to know where to troubleshoot.
Please advise and thanks in advance!
Current code is as follows:
navigator.geolocation.getCurrentPosition(
function(pos) {
$("#latitude").val(pos.coords.latitude);
$("#longitude").val(pos.coords.longitude);
}
)
Sound like you just need to enable high-accuracy locations:
navigator.geolocation.getCurrentPosition(
function(pos) {
$("#latitude").val(pos.coords.latitude);
$("#longitude").val(pos.coords.longitude);
},
null,
{enableHighAccuracy: true}
)

Get GeoLocation In The Browser

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.

Categories