不要再使用网上的globe的方法了,现在Cesium已经有两种方法可以获取到地形的高度了,分别为:
- sampleTerrain : 获取非精确的地形的高度
- sampleTerrainMostDetailed: 获取尽量精确的地形的高度
用法(注意,回调是异步的):
1.sampleTerrain(terrainProvider, level, positions)
Initiates a terrain height query for an array of Cartographic
positions by requesting tiles from a terrain provider, sampling, and interpolating. The interpolation matches the triangles used to render the terrain at the specified level. The query happens asynchronously, so this function returns a promise that is resolved when the query completes. Each point height is modified in place. If a height can not be determined because no terrain data is available for the specified level at that location, or another error occurs, the height is set to undefined. As is typical of the Cartographic
type, the supplied height is a height above the reference ellipsoid (such as Ellipsoid.WGS84
) rather than an altitude above mean sea level. In other words, it will not necessarily be 0.0 if sampled in the ocean. This function needs the terrain level of detail as input, if you need to get the altitude of the terrain as precisely as possible (i.e. with maximum level of detail) use sampleTerrainMostDetailed
.
翻译过来就是:
Cartographic
通过从地形提供者请求切片,采样和插值来 启动位置数组的地形高度查询。插值匹配用于在指定级别渲染地形的三角形。查询以异步方式发生,因此此函数返回在查询完成时解析的promise。每个点高度都会在适当位置进行修改 如果无法确定高度,因为该位置的指定级别没有可用的地形数据,或者发生其他错误,则高度设置为undefined。作为典型的 Cartographic
类型,提供的高度是参考椭球上方的高度(例如Ellipsoid.WGS84
而不是高于平均海平面的高度。换句话说,如果在海洋中采样,它不一定是0.0。如果您需要尽可能精确地获取地形的高度(即具有最高细节水平),则此功能需要将地形级别的细节作为输入sampleTerrainMostDetailed
。
实例:
// Query the terrain height of two Cartographic positions
var terrainProvider = Cesium.createWorldTerrain();
var positions = [
Cesium.Cartographic.fromDegrees(86.925145, 27.988257),
Cesium.Cartographic.fromDegrees(87.0, 28.0)
];
var promise = Cesium.sampleTerrain(terrainProvider, 11, positions);
Cesium.when(promise, function(updatedPositions) {
// positions[0].height and positions[1].height have been updated.
// updatedPositions is just a reference to positions.
});
2.sampleTerrainMostDetailed(terrainProvider, positions)
实例:
// Query the terrain height of two Cartographic positions
var terrainProvider = Cesium.createWorldTerrain();
var positions = [
Cesium.Cartographic.fromDegrees(86.925145, 27.988257),
Cesium.Cartographic.fromDegrees(87.0, 28.0)
];
var promise = Cesium.sampleTerrainMostDetailed(terrainProvider, positions);
Cesium.when(promise, function(updatedPositions) {
// positions[0].height and positions[1].height have been updated.
// updatedPositions is just a reference to positions.
});
实战:获取指定position的地形高度:
//x为longtitude,y为latitude,terrain是地形privider
var positions = [
Cesium.Cartographic.fromDegrees(x, y),
];
var promise = Cesium.sampleTerrainMostDetailed(terrain, positions);
Cesium.when(promise, function (updatedPositions) {
var terrainHeight = updatedPositions[0].height
console.log(terrainHeight );
});