I have a KML file which consist of only one KML Point with coordinates as <coordinates>-76.43594397,36.77823129,33.3030576</coordinates>
. I am trying to get this point's longitude latitude and altitude(height) by triggering a CLICK event on this KML point. I used the code below to trigger the click event:
var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(click) {
var pickedObject = viewer.scene.pick(click.position);
var position = pickedObject.id.position.getValue(viewer.clock.currentTime);
}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
By using this function, neither the position or the height is correct. Then I replaced with another snippet as below:
var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(click) {
var ellipsoid = viewer.scene.globe.ellipsoid;
var cartesian = viewer.camera.pickEllipsoid(click.position, ellipsoid);
var cartographic = ellipsoid.cartesianToCartographic(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(2);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(2);
var heightString = cartographic.height.toFixed(2);
}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
After utilizing this code on the click event on the KML point shown on the map, I can finally get the proper longitude and latitude, but the heightString always remains zero.
I have been stuck on this part for a long time and I really don't know which part is wrong. Please help me out of this. Is this snippet not suit for my situation or I have to convert the first coordinate to get the real altitude?
Thanks.
At a first guess, it might be due to your use of pickEllipsoid(). I don’t think that’s going to return a meaningful altitude value. You probably need to get the coordinates, then use sampleTerrain(). See prior discussions in this forum for more info, as well as http://stackoverflow.com/questions/28291013/get-ground-altitude-cesiumjs .
I have checked the demo and tried the coordinate on the demo page, the value is a negative with correct number. But when I paste the code into my project, I cannot get the value. How should i setup Cesium, do i have to load anything else apart from just the code from demo?