I know the title is a little confusing, I will try to express it clear below.
While I was trying to get the altitude of a click position, I found two working snippet that both get me the altitude. However, there is slightly different between the two results.
Below are the snippet I used to get the value.
## Snippet 1
var ellipsoid = viewer.scene.globe.ellipsoid; var pickedObject = viewer.scene.pick(click.position);
var cartesian = viewer.camera.pickEllipsoid(click.position, ellipsoid); var cartographic = ellipsoid.cartesianToCartographic(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
## Snippet 2
var ray = viewer.camera.getPickRay(click.position);
var cartesian = viewer.scene.globe.pick(ray, viewer.scene);
var cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian);
var long2 = Cesium.Math.toDegrees(cartographic.longitude);
var lat2 = Cesium.Math.toDegrees(cartographic.latitude);
However, after applied these two on the same click position, the coordinates I got are slightly different, which caused the altitudes are different. Can anybody explain to me a little bit about this difference?
Thanks.
camera.pickEllipsoid will return a position with height=0 regardless of the terrain. It queries the ellipsoid (which is Ellipsoid.WGS84 by default) for the position.
viewer.scene.globe.pick will send a ray from the camera to the click position and return the position of the first intersection with the globe mesh. This includes the height of the terrain (which may change depending on the level of the tile loaded).
You will probably want to use viewer.camera.pickEllipsoid if you don’t have terrain enabled and viewer.scene.globe.pick if you do.
Thanks for replying.
After a little test on these two methods, I found that even I apply them both at the same time with same click position, the longitude and latitude are still not the same.
As far as I think, since these two values are from one click action, which is the same window position, how come I got two different longitude and latitude? Did I make myself clear? This is really confusing to me, thanks.
Since the globe mesh is not going to exactly overlap with the ellipsoid, when you draw a straight line from the camera’s view outwards, it will presumably hit the globe mesh in a slightly different location than it would touch the ellipsoid. So, there should be a slight difference in both lat/lon and altitude.