HTML5 navigator.geolocation in Web Workers

Viewed 5050

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 + "\nline: " + e.lineno + "\nDescription: " + 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.

4 Answers
Related