简体   繁体   English

如何设置Cesium JS地图中心(坐标:纬度和经度)

[英]How to set Cesium JS map center (coordinates: latitude & longitude)

I would like to initialize cesium so that the map is centered on some specific coordinates instead of the default ones. 我想初始化cesium,以便地图以某些特定坐标为中心而不是默认坐标。 I have the following initialization code: 我有以下初始化代码:

var map = new Cesium.CesiumWidget('map-js');
map.centralBody.terrainProvider = new Cesium.CesiumTerrainProvider({
    url : 'http://cesiumjs.org/smallterrain'
});

Usually, with other mapping libraries, I would set the center on initialization, eg on mapbox: 通常,使用其他映射库,我会在初始化时设置中心,例如在mapbox上:

map = L.mapbox.map('map-js', 'api-key').setView([42.12, 12.45], 9);

How to do that with cesium ? 如何用做到这一点?

Try adding this after your first block of code above: 尝试在上面第一段代码后添加:

var scene = map.scene;
var ellipsoid = Cesium.Ellipsoid.WGS84;
var west = Cesium.Math.toRadians(-77.0);
var south = Cesium.Math.toRadians(38.0);
var east = Cesium.Math.toRadians(-72.0);
var north = Cesium.Math.toRadians(42.0);

var extent = new Cesium.Rectangle(west, south, east, north);
scene.camera.viewRectangle(extent, ellipsoid);

More examples are available in our Camera Demo . 我们的相机演示中提供了更多示例。

EDIT (May 2014): Due to Cesium API changes, .getCamera() is renamed .camera , the camera's .controller was removed and rolled into the camera itself, and Extent is renamed to Rectangle . 编辑(2014年5月):由于铯API的变化, .getCamera()被重命名.camera ,相机的.controller取出并滚入相机本身, Extent重命名为Rectangle The above code now reflects the new API. 上面的代码现在反映了新的API。 For a complete list of breaking changes, see CHANGES.md . 有关重大更改的完整列表,请参阅CHANGES.md

If you want to keep the current "zoom" (aka camera distance from ellipsoid) and only have lon/lat, you could call setView() and use the current camera height, like: 如果你想保持当前的“缩放”(也就是椭圆体的相机距离)并且只有lon / lat,你可以调用setView()并使用当前的相机高度,如:

    viewer.camera.setView({
        destination : Cesium.Cartesian3.fromDegrees(
            longitude,
            latitude,
            Cesium.Ellipsoid.WGS84.cartesianToCartographic(viewer.camera.position).height
        )
    });

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

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