Web Workers中的HTML5 navigator.geolocation [英] HTML5 navigator.geolocation in Web Workers

查看:138
本文介绍了Web Workers中的HTML5 navigator.geolocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的代码移动到web worker中的navigator.geolocation。



我尝试了Chrome和Safari,但在

上获得'undefined'var isGPSSupported = navigator.geolocation;



沮丧......他们在规范中表示,应该在网络工作者中支持导航器对象...

我的代码如下:

index.js

  var gpsWorker =新工人(app / gpsworker.js); 

gpsWorker.onmessage =函数(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();


函数initGeoLoc(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
self。 postMessage(Got position!);
});
} else {
self.postMessage(此平台不支持GPS);




$ b $ p
$ b

任何错误提示都将不胜感激。

解决方案

我之前有类似的问题,并且询问一个相关的问题。现在我相信我已经回答了您的问题(也是我的一个相关问题)。

navigator.geolocation仅在主线程中属于导航器,但不支持 t在工作者线程中属于导航器。



主要原因是即使工作线程中的导航器看起来与主线程中的导航器完全相同,但这两个导航器在C ++端有独立的实现。这就是为什么worker线程不支持navigator.geolocation的原因。



相关代码位于 Navigator.idl Chromium代码中的WorkerNavigator.idl 。您可以看到它们是.idl文件中的两个独立接口。而且它们在绑定的C ++端有独立的实现。导航器是 DOMWindow ,而WorkerNavigator是 WorkerGlobalScope 然而,在JavaScript方面,它们有相同的名称:navigator。由于两名导航员在两个不同的范围内,因此没有名称冲突。但是,当在JavaScript中使用API​​时,如果主线程和辅助线程具有相同的名称,那么人们通常会期望类似的行为。这就是模糊不清的情况。


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.

解决方案

I had similar question as yours before and asked a related question. Now I believe I have the answer to your question (and also one of my related questions).

navigator.geolocation belongs to navigator in the main thread only, but doesn't belong to navigator in the worker thread.

The main reason is that even though the navigator in worker thread looks exactly the same as the one in main thread, those two navigators have independent implementations on the C++ side. That is why navigator.geolocation is not supported in the worker thread.

The related code is in Navigator.idl and WorkerNavigator.idl in Chromium code. You can see that they are two independent interfaces in the .idl files. And they have independent implementations on the C++ side of the binding. Navigator is an attribute of DOMWindow, while WorkerNavigator is an attribute of WorkerGlobalScope.

However, on the JavaScript side, they have the same name: navigator. Since the two navigators are in two different scopes, there is no name conflict. But when using the APIs in JavaScript, people usually expect similar behavior on both main and worker threads if they have the same name. That's how the ambiguity happens.

这篇关于Web Workers中的HTML5 navigator.geolocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆