【问题标题】:Cesium - drawing polygon using camera Lat-Lon-Alt positionsCesium - 使用相机 Lat-Lon-Alt 位置绘制多边形
【发布时间】:2016-07-16 06:31:09
【问题描述】:

这个问题与这两个有关:

  1. Cesium how to scale a polygon to match Lat-Lon positions while zoom-in/zoom-out
  2. Cesium - using camera to scale a polygon to match Lat-Lon positions while zoom-in/zoom-out

我遵循的从相机获取纬度-经度-alt 位置的示例代码位于gold standard that appears to be baked into the existing camera controller。使用此代码,我可以从相机的距离检索纬度经纬度位置,以获得几乎与所选原始纬度经度位置和地球表面以上高度完全相同的值。完美!

所有示例和文档都展示了使用度数或度数中的点创建多边形。

现在呢?也许我遗漏了一些东西,但我认为的目的是能够使用特定的 x、y、z 坐标创建多边形,以便在放大、缩小和相机运动。既然我有了这些值,那么用这些值绘制多边形的秘诀是什么?

仅供参考,这些是我目前拥有的价值:


==========================新信息================= ==========

redPolygon 的代码有效:

var redPolygon = viewer.entities.add({
    name : 'Red polygon on surface',
    polygon : {
        hierarchy : Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0,
                                                        -115.0, 32.0,
                                                        -102.0, 31.0,
                                                        -102.0, 35.0,
                                                        -102.0, 35.0]),
        material : Cesium.Color.RED
    }
});

viewer.flyTo(redPolygon);

bluePolygon 的代码不起作用:

var bluePolygon = viewer.entities.add({
    name : 'Blue polygon on surface',
    polygon : {
        //hierarchy: collection.latlonalt,
        hierarchy: Cesium.Cartesian3.fromArray(collection.latlonalt),
        material : Cesium.Color.BLUE
    }
});

viewer.flyTo(bluePolygon);

如果我使用hierarchy: collection.latlonalt,,我会收到以下错误:

所以我将代码更改为 hierarchy: Cesium.Cartesian3.fromArray(collection.latlonalt), 其中 collection.latlonalt 是我的 Cartesian3 数组:

但是什么都画不出来。没有错误。这是我在控制台中看到的:

只是为了测试,我尝试将 z 位置添加到 redPolygon 并将 .fromDegreesArray 更改为 .fromArray,如下所示:

var redPolygon = viewer.entities.add({
    name : 'Red polygon on surface',
    polygon : {
        hierarchy : Cesium.Cartesian3.fromArray([-115.0, 37.0, 10.0,
                                                 -115.0, 32.0, 10.0,
                                                 -102.0, 31.0, 10.0,
                                                 -102.0, 35.0, 10.0,
                                                 -102.0, 35.0, 10.0]),
        material : Cesium.Color.RED
    }
});

viewer.flyTo(redPolygon);

那也没用。

【问题讨论】:

    标签: javascript node.js camera terrain cesium


    【解决方案1】:

    Cesium 有 Polygon Demo 使用的类似 Cartesian3.fromDegreesArray 的辅助函数,但是,现在您已经掌握了实际的 Cartesian3 值,就不需要这些辅助函数了。

    例如,多边形演示代码如下所示:

    var redPolygon = viewer.entities.add({
        name : 'Red polygon on surface',
        polygon : {
            hierarchy : Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0,
                                                            -115.0, 32.0,
                                                            -107.0, 33.0,
                                                            -102.0, 31.0,
                                                            -102.0, 35.0]),
            material : Cesium.Color.RED
        }
    });
    

    在上面的代码中,fromDegreesArray 在这种情况下只需要一个包含 5 个 lot/lan 值对的列表,并将它们转换为一个包含 5 个 Cartesian3 类实例的 JavaScript 数组。这个由 5 个笛卡尔 3 组成的数组随后作为hierarchy 的值存储在多边形定义中。如果您在运行时检查该定义,您会发现原始 lon/lat 值已被丢弃,由实际的 Cartesian3 替换,这要归功于辅助函数。

    因此,在您的代码中,您需要一个用户迄今为止单击的 Cartesian3 数组。这从空数组开始,您需要收集至少三次点击,将每次点击转换为 Cartesian3,如您在上面的问题中所示,并将 push 该值转换为数组。一旦数组累积了 3 次或更多点击,您就可以将该数组作为多边形定义的 hierarchy 字段传递。

    通过这种方式,您避免了调用fromDegreesArray,因为您的点击处理程序正在执行更详细的工作来收集每次点击的准确笛卡尔位置。这种收集必须在每次点击时发生,以防相机在点击之间移动。因此,“进行中”的数组必须在两次点击之间存活,直到所有点击都被收集并可以创建一个多边形。

    编辑:这是我试图描述的代码结构示例。我没有在这里显示实际的点击处理程序,因为您似乎已经从鼠标点击中获得了 Cartesian3 值。相反,我展示了三个用于创建多边形的此类值。

    var viewer = new Cesium.Viewer('cesiumContainer');
    
    // Create an empty array of click positions at the start.
    var clickPositions = [];
    
    // When the first mouse click is received, convert to Cartesian3, and push it into the array.
    var click1 = new Cesium.Cartesian3(-2155350.2, -4622163.4, 3817393.1);
    clickPositions.push(click1);
    
    // Later, more mouse clicks are received and pushed into the array.
    var click2 = new Cesium.Cartesian3(-2288079.8, -4906803.1, 3360431.4);
    clickPositions.push(click2);
    
    var click3 = new Cesium.Cartesian3(-1087466.8, -5116129.4, 3637866.9);
    clickPositions.push(click3);
    
    // Finally, draw the polygon.
    var redPolygon = viewer.entities.add({
        name : 'Red polygon on surface',
        polygon : {
            hierarchy : clickPositions,
            material : Cesium.Color.RED
        }
    });
    

    请注意,当 clickPositions 分配给 hierarchy 时,它没有任何反应。 Cartesian3 值的数组在这里已经是 Cesium 需要的形式了。

    【讨论】:

    • 嘿,emacky!也许我仍然做错了什么,但它不是画画。在下面查看我的编辑NEW INFORMATION
    • 嗨,露西小姐,我在答案的底部添加了一个新代码块。
    • 太棒了!!!!我在其他几个答案中看到“此错误几乎总是由 NaN 或未定义值引起”。在某个时候,我开始怀疑我是否需要加分,但在我的测试中我没有得到那个。这是完美的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多