在HTML5 Web Worker中使用Geolocation

时间:2023-02-08 19:37:21

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.

我想使用HTML5网络工作者定期向一个宁静的服务器报告经度和纬度 - 即使标签不在焦点上。我的问题是,我通常访问HTML5地理定位功能的方式在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:

我尝试过将地理位置对象从我的主文件发送到Web工作者。但是,这不起作用,我得到以下错误:

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

未捕获的DataCloneError:无法在'Worker'上执行'postMessage':无法克隆对象。

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

接下来,我尝试导入一个JavaScript库,该库提供名为Geolocator的地理定位服务。但后来我发现我无法访问worker中的库(我认为因为worker没有访问DOM的权限)。

So, in an attempt to combat 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.

因此,为了解决这个问题,我将这个JavaScript库的源代码直接添加到了worker文件中,但这不起作用,因为我无法访问在Geolocator源中重复使用的窗口。

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

我正在尝试做什么?有什么替代品我可以使用吗?

2 个解决方案

#1


4  

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.

Worker包含一个不同的导航器对象,称为WorkerNavigator,它只包含主线程中导航器属性的子集。

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.

由于WorkerNavigator缺少Geolocation对象,并且Geolocation对象无法序列化为worker,因此您似乎无法在Web worker中使用Geolocation。相反,您必须从主线程进行报告。

#2


0  

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.

您可以在此问题中找到您的提案的工作示例。它目前仅适用于Firefox的背景(出于错误的原因),并且在我们在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.

但是和你一样,在现实世界中存在许多这样的要求,我相信我的建议会遇到大多数(如果不是全部的话)。

#1


4  

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.

Worker包含一个不同的导航器对象,称为WorkerNavigator,它只包含主线程中导航器属性的子集。

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.

由于WorkerNavigator缺少Geolocation对象,并且Geolocation对象无法序列化为worker,因此您似乎无法在Web worker中使用Geolocation。相反,您必须从主线程进行报告。

#2


0  

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.

您可以在此问题中找到您的提案的工作示例。它目前仅适用于Firefox的背景(出于错误的原因),并且在我们在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.

但是和你一样,在现实世界中存在许多这样的要求,我相信我的建议会遇到大多数(如果不是全部的话)。