javascript - Using Geolocation in a HTML5 Web Worker - Stack Overflow

I want to use a HTML5 web worker to report longitude and latitude to a restful server at regular interv

I want to use a HTML5 web worker to report longitude and latitude to a restful server at regular intervals - even when the tab is not in focus. My problem is that the way that I would normally access the HTML5 geolocation function is not available in the web worker.

What I have tried is sending the geolocation object, from my main file to the web worker. However, that didn't work and I got the following error:

Uncaught DataCloneError: Failed to execute 'postMessage' on 'Worker': An object could not be cloned.

Next I tried importing a JavaScript library which provides geolocation services called Geolocator. But then I found that I do not have access to the library in the worker (I think because the worker doesn't have access to the DOM).

So, in an attempt to bat this, I added the source of this JavaScript library directly into the worker file but this didn't work because I don't have access to window which is used repeated throughout the source of Geolocator.

Is what I am trying to do possible? Is there any alternatives which I can use?

I want to use a HTML5 web worker to report longitude and latitude to a restful server at regular intervals - even when the tab is not in focus. My problem is that the way that I would normally access the HTML5 geolocation function is not available in the web worker.

What I have tried is sending the geolocation object, from my main file to the web worker. However, that didn't work and I got the following error:

Uncaught DataCloneError: Failed to execute 'postMessage' on 'Worker': An object could not be cloned.

Next I tried importing a JavaScript library which provides geolocation services called Geolocator. But then I found that I do not have access to the library in the worker (I think because the worker doesn't have access to the DOM).

So, in an attempt to bat this, I added the source of this JavaScript library directly into the worker file but this didn't work because I don't have access to window which is used repeated throughout the source of Geolocator.

Is what I am trying to do possible? Is there any alternatives which I can use?

Share Improve this question edited Mar 22, 2016 at 19:24 Cjmarkham 9,6896 gold badges52 silver badges85 bronze badges asked Mar 22, 2016 at 19:21 HaychHaych 9422 gold badges18 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

The navigator object in the main thread contains a geolocation property, which is how you access all geolocation functionality.

Workers contain a different navigator object, known as WorkerNavigator which only contains a subset of the properties of the navigator in the main thread.

Because the WorkerNavigator lacks a Geolocation object and the Geolocation object can't be serialized into a worker, it appears you can't use Geolocation inside of a web worker. Instead, you'll have to do your reporting from the main thread.

There is a working example of your proposal that can be found at this question. It currently only works in background on Firefox (for the wrong reasons) and is unlikey to get implemented until we have substantial regime change at W3C/IETF.

But like you, there are many such requirements out there in the real world and I believe my proposal meets most if not all of them.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744623331a4584482.html

相关推荐

  • javascript - Using Geolocation in a HTML5 Web Worker - Stack Overflow

    I want to use a HTML5 web worker to report longitude and latitude to a restful server at regular interv

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信