I'm trying to find the distance between a mouseclick and the nearest of a set of billboards. My code is as follows:
var smallestDistance = -1;
var currentDistance = -1;
var nearestPeak;
var peakPosition;
var mousePosition = new Cesium.Cartesian3(ev.clientX, ev.clientY, 0.0);
for (var i = 0; i < peaks.length; i++) {
peakPosition = peaks[i].position;
currentDistance = Cesium.Cartesian3.distance(mousePosition, peakPosition);
console.log( 'CESIUM: Distance to ' + peaks[i].name + ': ' + currentDistance + ' units.' );
if (currentDistance < smallestDistance) {
smallestDistance = currentDistance;
nearestPeak = displayedPeaks[i];
}
}
console.log( 'CESIUM: nearest peak is peak ' + nearestPeak.peakId );
if (smallestDistance != -1 && smallestDistance < 100000) {
selectedPeak = nearestPeak;
}
The issue appears in the first console.log statement, where it displays:
CESIUM: Distance to peak 1: NaN units.
According to Cesium's documentation, it should be returning a number. I could swear I did something similar in a past project and didn't have this issue. I have a strong suspicion that I am missing and/or forgetting something very basic. Can anyone advise?
EDIT: I did some further debugging, and I am finding that peakPosition.x and peakPosition.y are both undefined, where peakPosition = peaks[i].position. peaks[i] is still rendering as a billboard on the globe, and peakPosition is an object. According to the Cesium documentation, it SHOULD be an object of type Cartesian3, which should have x and y members. So why would those members be undefined?
EDIT 2: I tried replacing this line:
peakPosition = peaks[i].position;
with this:
peakPosition = new Cesium.Cartesian3(this.peaks[i].position.x, this.peaks[i].position.y, 0.0);
and its x, y, and z members are always 0, 0, and 0. I can't figure this out for the life of me.
So, per our discussion we have discovered:
peaks
is an array of Cesium.Entity
s.
and
the position
field of a Cesium.Entity
is a Cesium.PositionProperty
, which is an interface that has a method getValue
, accepting a Cesium.JulianDate
as its first parameter. So, putting all that together, the only change you need is:
peakPosition = peaks[i].position;
to
peakPosition = peaks[i].position.getValue(new Cesium.JulianDate());