简体   繁体   English

如何缩放/移动相机以在Cesium.js中显示多边形(使视口适应多边形边界)

[英]How to zoom/move camera to display a polygon in Cesium.js (fit viewport to polygon bounds)

Ok, maybe I'm blind because clearly this is not advanced functionality.. but I'm looking at the Camera docs for Cesium.js , and I can't seem to find how you simply zoom/move the view to show a polygon? 好的,也许我是盲人,因为显然这不是高级功能。.但是我正在查看Cesium.jsCamera文档 ,而且似乎无法找到如何简单地缩放/移动视图以显示多边形?

I can position the camera to show the poly using the mean lat/lon values, like: 我可以使用平均纬度/经度值定位相机以显示多边形,例如:

viewer.camera.setView({
    position : Cesium.Cartesian3.fromDegrees(lonMean, latMean, 45000.0)
});

But that's rubbish; 但是那是垃圾。 I don't get the zoom... I want the zoom/viewport to be adjusted to fit the polygon. 我没有缩放... 我想要调整缩放/视口以适合多边形。

Can anybody point out what I must be overlooking... 谁能指出我必须忽略的地方...

Assuming you have a polygon defined in the Entity API, then you can simply call viewer.zoomTo(entity) or viewer.flyTo(entity) . 假设您在Entity API中定义了一个多边形,那么您可以简单地调用viewer.zoomTo(entity)viewer.flyTo(entity) This works with any type of entity visualization, not just polygons, and is discussed in Cesium's Visualizing Spatial Data tutorial . 此功能适用于任何类型的实体可视化,而不仅限于多边形,并且在Cesium的“ 可视化空间数据”教程中进行了讨论。

Take a look at the Sandcastle Camera Demo . 看看Sandcastle Camera Demo Pull-down the "Camera options" drop box, and select either "Fly to Rectangle" or "View a rectangle." 下拉“相机选项”下拉框,然后选择“飞行到矩形”或“查看矩形”。 The camera will fly or snap to the rectangle, and the code editor shows how this is done. 摄像机将飞行或捕捉到矩形,然后代码编辑器显示该操作。 Take a look through the other Sandcastle demos too, they contain lots of sample code for common actions in Cesium. 还要看一下其他Sandcastle演示,它们包含许多用于Cesium中常见操作的示例代码。

Here's the FlyTo code: 这是FlyTo代码:

var west = -90.0;
var south = 38.0;
var east = -87.0;
var north = 40.0;
var rectangle = Cesium.Rectangle.fromDegrees(west, south, east, north);

viewer.camera.flyTo({
    destination : rectangle
});

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

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