I'm looking for a Cesium Guru for a little help finding what I need. I'm new to Cesium but I've been working with the tutorials and some existing code that I've inherited.
In my Cesium app, I enter my address and the view zooms in to my street. Yay! Then I zoom in closer, so I can draw a polygon around my house. The existing code does this very well. However when I zoom out and then zoom in again, my polygon does not stay true to the Lat-Lon position of my house.
Does Cesium contain a utility to scale pixels to lat-lon coordinates or do I need to use something like distanceToBoundingSphere(boundingSphere) and calculate it myself? I only want the x,y coordinates; I don't care about height at all.
I have been looking around in the demos and tutorials and so far haven't found what I think I'm looking for. Maybe I have found something close but I don't know enough yet to know whether I've found it or not. Help!
============================ CODE ==================================
Collecting the positions for the polygon:
A singleClick only captures the coordinates for that point and draws a polyline as user drags mouse to a new point. Hence, a bunch of polylines and a collection of coordinates for each point.
positionHandler.setInputAction(function (click) {
cartesian = scene.camera.pickEllipsoid(click.position, ellipsoid);
if (cartesian) {
var setCartographic = ellipsoid.cartesianToCartographic(cartesian);
asset.latlonalt.push(
Cesium.Math.toDegrees(setCartographic.latitude).toFixed(15),
Cesium.Math.toDegrees(setCartographic.longitude).toFixed(15),
Cesium.Math.toDegrees(setCartographic.height).toFixed(15)
);
lla.push(Cesium.Math.toDegrees(setCartographic.longitude), Cesium.Math.toDegrees(setCartographic.latitude));
if (lla.length >= 4) {
self.loggingMessage((lla.length / 2) + ' Points Added');
}
Cesium.sampleTerrain(terrainProvider, 11, [cartographic])
.then(function (updatedPositions) {
asset.latlonalt[2] = updatedPositions[0].height;
stage = 1;
});
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
A doubleClick then takes the coordinates captured in the singleClick function and calls self.createAsset('add', asset) to create the polygon.
positionHandler.setInputAction(function (doubleClick){
if (asset.shape == 'Polygon') {
var len = asset.latlonalt.length;
if(len > 9) {
asset.rad = (len / 3);
console.log("Creating Asset");
self.loggingMessage("Creating Asset");
socket.emit('newElement', asset.cType, asset);
self.createAsset('add', asset);
viewer.entities.remove(entity);
viewer.entities.remove(newCircle);
viewer.entities.remove(newPolygon);
viewer.entities.remove(newOutline);
positionHandler = positionHandler && positionHandler.destroy();
}else{
console.log('3+ Positions Required');
loggingMessage('3+ Positions Required.');
}
}
}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
Creating the polygon:
var newPolygon = viewer.entities.add({
name : asset.id,
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray(vertices),
material : rgba[0],
outline : true,
outlineColor : rgba[1]
}
});
var newLabel = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(asset.latlonalt[1], asset.latlonalt[0], 1000),
name: asset.id,
label: {
text: asset.name,
font : '16px Helvetica'
}
});
var newPoint = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(asset.latlonalt[1], asset.latlonalt[0], 0),
name: asset.id,
point : {
pixelSize : 5,
color : Cesium.Color.RED,
outlineColor : Cesium.Color.RED,
outlineWidth : 2
}
});
self.currentGeometry[asset.id] = {shape: newPolygon, label: newLabel, point: newPoint};
It looks like we're using Terrain (I think).
Which numbers should I be interested in:
When the coordinates are collected, only the first z is not zeros:
I take that value and populated the other z values:
Now that I have added the z values something is erroring out in the createAsset method. I need to track that problem down to see the results. Right now, it looks like this:
REALLY BIG and the outlines are not getting removed.