【问题标题】:WebWorldWind Renderable Position UpdateWebWorldWind 可渲染位置更新
【发布时间】:2018-01-24 21:11:08
【问题描述】:

我正在尝试每隔一段时间在 web worldwind 地球上“移动”可渲染对象。为了说明我遇到的问题,我做了一个小例子。

这可行(但效率低下):

    var myVar = setInterval(myTimer, 5000);

    function myTimer() {


        shapesLayer.removeRenderable(shape);
        shape = new WorldWind.SurfaceCircle(new WorldWind.Location(shape.center.latitude+1, shape.center.longitude), 200e3, attributes);
        shapesLayer.addRenderable(shape);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }

这是我想做的,但形状不动:

    var myVar = setInterval(myTimer, 5000);

    function myTimer() {

        shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }

我需要在可渲染对象上设置一个标志以使其刷新吗?

这是我一直在使用的完整 SurfaceShapes.js 文件(基于此 http://worldwindserver.net/webworldwind/examples/SurfaceShapes.html):

/*
 * Copyright (C) 2014 United States Government as represented by the Administrator of the
 * National Aeronautics and Space Administration. All Rights Reserved.
 */
/**
 * Illustrates how to display SurfaceShapes.
 *
 * @version $Id: SurfaceShapes.js 3320 2015-07-15 20:53:05Z dcollins $
 */

requirejs(['../src/WorldWind',
        './LayerManager'],
    function (ww,
              LayerManager) {
        "use strict";

        // Tell World Wind to log only warnings.
        WorldWind.Logger.setLoggingLevel(WorldWind.Logger.LEVEL_WARNING);

        // Create the World Window.
        var wwd = new WorldWind.WorldWindow("canvasOne");

        /**
         * Added imagery layers.
         */
        var layers = [
            {layer: new WorldWind.BMNGLayer(), enabled: true},
            {layer: new WorldWind.BingAerialWithLabelsLayer(null), enabled: true},
            {layer: new WorldWind.CompassLayer(), enabled: true},
            {layer: new WorldWind.CoordinatesDisplayLayer(wwd), enabled: true},
            {layer: new WorldWind.ViewControlsLayer(wwd), enabled: true}
        ];

        for (var l = 0; l < layers.length; l++) {
            layers[l].layer.enabled = layers[l].enabled;
            wwd.addLayer(layers[l].layer);
        }

        // Create a layer to hold the surface shapes.
        var shapesLayer = new WorldWind.RenderableLayer("Surface Shapes");
        wwd.addLayer(shapesLayer);

        // Create and set attributes for it. The shapes below except the surface polyline use this same attributes
        // object. Real apps typically create new attributes objects for each shape unless they know the attributes
        // can be shared among shapes.
        var attributes = new WorldWind.ShapeAttributes(null);
        attributes.outlineColor = WorldWind.Color.BLUE;
        attributes.drawInterior = false;
        attributes.outlineWidth  = 4;
        attributes.outlineStippleFactor = 1;
        attributes.outlineStipplePattern  = 0xF0F0;    

        var highlightAttributes = new WorldWind.ShapeAttributes(attributes);
        highlightAttributes.interiorColor = new WorldWind.Color(1, 1, 1, 1);


        // Create a surface circle with a radius of 200 km.
        var shape = new WorldWind.SurfaceCircle(new WorldWind.Location(35, -120), 200e3, attributes);
        shape.highlightAttributes = highlightAttributes;
        shapesLayer.addRenderable(shape);

        // Create a layer manager for controlling layer visibility.
        var layerManger = new LayerManager(wwd);

        // Now set up to handle highlighting.
        var highlightController = new WorldWind.HighlightController(wwd);

        var myVar = setInterval(myTimer, 5000);

        function myTimer() {

            //Shape doesn't move

            shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

            //Shape "moves" but is inefficient

            //shapesLayer.removeRenderable(shape);
            //shape = new WorldWind.SurfaceCircle(new WorldWind.Location(shape.center.latitude+1, shape.center.longitude), 200e3, attributes);
            //shapesLayer.addRenderable(shape);

            console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

            wwd.redraw();
        }
    }
);

【问题讨论】:

  • 你测试Renderable.setPosition(Position position) 吗?我认为它适用于您的情况。如果您的shapeRenderable,您可以使用shape.setPosition(...)

标签: javascript worldwind webworldwind


【解决方案1】:

文档说 renderables 变量是只读的,但我认为这是可能的。
修改代码

shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

index = ShapesLayer.renderables.indexOf(shape);
ShapesLayer.renderables[index].center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

我认为 ShapesLayer.addRenderable 创建花药形状。

如果你觉得这样做不好,你可以使用这种方式

    RenderableLayer.prototype.changeRenderable= function (prevRenderable, nextRenderable) {

      index = ShapesLayer.renderables.indexOf(prevRenderable);
      ShapesLayer.renderables[index].center = nextRenderable;
    };

主要代码

 ShapesLayer.changeRenderable(shape, new WorldWind.Location(shape.center.latitude+1, shape.center.longitude));

文档:https://nasaworldwind.github.io/WebWorldWind/layer_RenderableLayer.js.html

【讨论】:

  • 我在 WebWorldWind (nasaworldwind.github.io/WebWorldWind) 的 API 中没有看到 ShapesLayer。我相信您可能正在查看 WorldWind Java,而不是我在问题中的 WebWorldWind。
【解决方案2】:

如果其他人在看这个,我找到了一个更好的解决方案,通过将 isPrepared 设置为 false 并将 _boundaries 设置为 undefined 来强制它重新计算中心位置。

   var myVar = setInterval(myTimer, 5000);

    function myTimer() {

        shape.isPrepared = false;
        shape._boundaries = undefined;

        shape.center = new WorldWind.Location(shape.center.latitude+1, shape.center.longitude);

        console.log(" new pos "+shape.center.latitude + " "+shape.center.longitude);

        wwd.redraw();
    }

【讨论】:

  • 很高兴你得到了一行答案,shape._boundaries = undefined,为什么需要它?
猜你喜欢
  • 2020-09-17
  • 2019-02-26
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多