1

我正在尝试使用 Dart 将 .czml 文件加载到 Cesium 地图中。javascript代码正常工作,看起来像这样

    var viewer = new Cesium.Viewer('cesiumContainer');
    viewer.extend(Cesium.viewerDynamicObjectMixin);
    viewer.dataSources.removeAll();

    var czmlDataSource = new Cesium.CzmlDataSource();

    czmlDataSource.loadUrl('Vehicle.czml').then(function() {
        viewer.dataSources.add(czmlDataSource);
    });

这是我尝试使用的转换为 Dart 的代码。

    void drawCzmlStream() {    
        _viewer.callMethod('extend', [context['Cesium']['viewerDynamicObjectMixin']]);
        _viewer['dataSources'].callMethod('removeAll');

        var czmlDataSource = new JsObject(context['Cesium']['CzmlDataSource']);

        czmlDataSource.callMethod('loadUrl', ['Vehicle.czml']).then(displaySource(czmlDataSource));

        print("finished");
    }

    void displaySource(dataSource) {
        _viewer['dataSources'].callMethod('add', [dataSource]);
        print("finished displaying");
    }

代码似乎执行正确,加载czml文件中的数据并在地图上绘制,并打印“完成显示”。但是,在打印语句之后会出现错误,并且永远不会显示第二个“完成”。我怀疑这与我使用 .then 调用的方式有关。我正在使用 AngularDart 以及调用 drawCzmlStream() 的 CesiumController。

这是控制台显示:

finished displaying

Undefined function drawCzmlStream

STACKTRACE:
#0      DynamicClosureMap.lookupFunction.<anonymous closure> (package:angular/core/parser/parser_dynamic.dart:51:11)
#1      CallMember.eval (package:angular/core/parser/eval_calls.dart:51:25)
#2      DynamicExpression.eval (package:angular/core/parser/dynamic_parser.dart:53:30)
#3      DynamicExpression.eval (package:angular/core/parser/dynamic_parser.dart:54:7)
#4      BoundExpression.call (package:angular/core/parser/syntax.dart:59:36)
#5      NgEvent.initListener.<anonymous closure> (package:angular/directive/ng_events.dart:154:39)
#6      _rootRunUnary (dart:async/zone.dart:734)
#7      _rootRunUnary (dart:async/zone.dart:735)
#8      _rootRunUnary (dart:async/zone.dart:735)
#9      _ZoneDelegate.runUnary (dart:async/zone.dart:462)
#10     _onRunUnary.<anonymous closure> (package:angular/core/zone.dart:114:63)
#11     VmTurnZone._onRunBase (package:angular/core/zone.dart:98:16)
#12     _onRunUnary (package:angular/core/zone.dart:114:17)
#13     _ZoneDelegate.runUnary (dart:async/zone.dart:462)
#14     _CustomizedZone.runUnary (dart:async/zone.dart:667)
#15     _BaseZone.runUnaryGuarded (dart:async/zone.dart:582)
#16     _BaseZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:608)
4

1 回答 1

2

谢谢本杰明,你的建议是正确的。这是最终的飞镖代码。

  void drawCzmlStream() {    
      _viewer.callMethod('extend', [context['Cesium']['viewerDynamicObjectMixin']]);
      _viewer['dataSources'].callMethod('removeAll');

      var czmlDataSource = new JsObject(context['Cesium']['CzmlDataSource']);
      var loaded = czmlDataSource.callMethod('loadUrl', ['../czml/Vehicle.czml']);
      loaded.callMethod('then', [(_) => displaySource(czmlDataSource)]);

      print("finished");
  }

  void displaySource(dataSource) {
    _viewer['dataSources'].callMethod('add', [dataSource]);
    print("finished displaying");
  }
于 2014-05-15T18:53:26.123 回答