How to set the center of cesium JS (coordinates: latitude and longitude)

I would like to initialize cesium so that the map is focused on specific coordinates instead of the standard ones. 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, for example, on mapbox:

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

How to do this with cesium ?

+8
javascript cesium
source share
2 answers

Try adding this after the 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); 

Additional examples are available in our camera demo .

EDIT (May 2014): Due to changes in the cesium API, .getCamera() renamed to .camera , the .controller camera was removed and flipped to the camera itself, and Extent renamed to Rectangle . The above code now reflects the new API. For a complete list of gap changes, see CHANGES.md .

+6
source share

If you want to keep the current "zoom" (or the distance from the camera from the ellipsoid) and only lon / lat, you can call setView () and use the current camera height, for example:

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

All Articles