I am trying to move my code for navigator.geolocation in a web worker.
I tried it with Chrome and Safari but getting 'undefined' on
var isGPSSupported = navigator.geolocation;
Frustrated... they said in specification that 'navigator' object should be supported in web workers...
My code is below:
index.js
var gpsWorker = new Worker("app/gpsworker.js");
gpsWorker.onmessage = function (e) {
alert(e.data);
};
gpsWorker.postMessage("Start GPS!");
gpsWorker.onerror = function (e) {
alert("Error in file: " + e.filename + "
line: " + e.lineno + "
Description: " + e.message);
};
gpsworker.js
self.onmessage = function (e) {
initGeoLoc();
}
function initGeoLoc() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
self.postMessage("Got position!");
});
} else {
self.postMessage("GPS is not supported on this platform.");
}
}
Any hint on what is wrong will be greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…