简体   繁体   English

如何将繁重的地理编码转移到网络工作者?

[英]how to move heavy geocoding to a web-worker?

I have very big list of geo points which I want to translate to coordinates with the geocoder and the best way I think is to move the task to a web worker, otherwise the Firefox times out and never loads the page. 我有大量的地理位置要转换为与Geocoder协调的地理位置,我认为最好的方法是将任务移至网络工作者,否则Firefox超时并且永远不会加载页面。

// the main html file:
var myWorker = new Worker('datapointscollection.js');
  myWorker.onmessage = function(e) {
      document.getElementById('loadingStatus').innerHTML = count + " elements from " + all + "are ready.";
      if (count == all) {
        myWorker.terminate();
        myWorker = undefined;
      }
  };

   myWorker.postMessage([geocodingParams]);

// the worker js file:
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-core.js");
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-service.js");
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-ui.js");
self.importScripts("http://js.api.here.com/v3/3.0/mapsjs-mapevents.js");
self.addEventListener(
'message', 
function(e) {
  var count = 0;
  var all = 0;

  // Initialize the platform object:
  var platform = new H.service.Platform({
  'app_id': 'myappID',
  'app_code': 'myappCODE'
  });

  var geocoder = platform.getGeocodingService();

  var onResult = function(result) {};

  var findLocations = function(geocodingParams) {
    var i=0;
    all = geocodingParams.length;
    for (i=0; i<geocodingParams.length; i++) {
      geocoder.geocode(
        geocodingParams[i], 
        onResult, 
        function(e){
            alert(e);
        } );
      count = i;
      self.postMessage(count, all);
    }
  };

  findLocations(e.data[0]);
}, 
false);

I tried different approaches, but executing the worker script fails with different errors. 我尝试了不同的方法,但是执行工作脚本失败并出现不同的错误。 The last problem is ReferenceError: document is not defined in mapsjs-core.js:158:623. 最后一个问题是ReferenceError:mapsjs-core.js:158:623中未定义文档。 and after a while another error: NetworkError: A network error occurred. 不久后又出现另一个错误:NetworkError:发生网络错误。 from datapointscollection.js:1 来自datapointscollection.js:1

For huge number of geocodes, you should consider batch geocoding. 对于大量的地理编码,您应该考虑批量地理编码。 Check developer's guide at developer.here.com 在developer.here.com上查看开发人员指南。

It looks like the files you are importing into your worker depend on a DOM existing. 看来您要导入到工作器中的文件取决于现有的DOM。 Your web worker doesn't have a DOM, so you will have to use dependencies that don't need a DOM (if it will work in node, it will work without a DOM). 您的Web工作者没有DOM,因此您将必须使用不需要DOM的依赖项(如果它可以在节点中工作,则无需DOM就可以工作)。 Check the documentation for your dependencies to see if there a version that works in node or doesn't need a DOM, and use that version in your web worker. 检查文档以了解您的依赖项,以查看是否存在可以在节点中使用或不需要DOM的版本,并在Web Worker中使用该版本。 (It might be just the mapsjs-service.js file. see if you can get away with just that). (它可能只是mapsjs-service.js文件。请看您是否可以解决这个问题)。

See also Web Workers API 另请参阅Web Workers API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM