Distance

Hello! I am very new to Cesium and am in need of help with a distance function. The way I would set this up is by using a post I found previously by Hypersonic with this distance function:

var viewer = new Cesium.Viewer('cesiumContainer');
var geodesic = new Cesium.EllipsoidGeodesic();
var cart1 = new Cesium.Cartographic(80,40);
var cart2 = new Cesium.Cartographic(90,50);
geodesic.setEndPoints(cart1, cart2);
console.log(geodesic.surfaceDistance);

Instead of using set points I would like to get the points by a combination of mouse click and press of a keyboard button, but nothing seems to be working. The code I have currently is

if (Cesium.ScreenSpaceEventType.Right_Click && flags.one)
{
window.alert(message);
}

Cesium is still not responding to this, but my experience with this and javascript is minimal so any help would be great thanks!

I’m not sure in what context you are calling that if block, but it’s incorrect. Here’s a small complete example you can paste into sandcastle that spits out the surface distance whenever 2 right clicks are made. You can add a KeyboardEventModifier as an extra parameter after the RIGHT_CLICK if you want to require shift/ctrl/alt. For example KeyboardEventModifier.SHIFT. It should be straightforward to enhance this code to create a point/line on each click for visual feedback.

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var scene = viewer.scene;

var ellipsoid = scene.globe.ellipsoid;

var handler;

var positions = ;

// Mouse over the globe to see the cartographic position

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);

handler.setInputAction(function(movement) {

var cartesian = viewer.camera.pickEllipsoid(movement.position, ellipsoid);

if (cartesian) {

var cartographic = ellipsoid.cartesianToCartographic(cartesian);

positions.push(cartographic);

if(positions.length === 2) {

var geodesic = new Cesium.EllipsoidGeodesic(positions[0], positions[1]);

console.log(geodesic.surfaceDistance.toFixed(2) + ’ m’);

positions.length = 0;

}

}

}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

Thank you this works great, I am trying to figure out how to apply this to areas off the earth as well and to automatically update for a tracked moving object like a satellite, is that currently a feature possible in Cesium?

If you want to track off-earth distance you would want to use straight-line Cartesian3.distance rather than EllipsoidGeodesic. Other than that, it should be pretty straight forward to adapt this code to handle the cases you want.