How to get cartographic/cartesian3 coordinates on mouse click

I think pickPosition will give you that if you click on the terrain:

https://cesiumjs.org/Cesium/Build/Documentation/Scene.html?classFilter=scene#pickPosition

It does not help.
I am trying to put marker with right click and get his location in terrain mode.

When I am zooming the map closer to the ground and right click for getting the location, the marker doesn’t put where I clicked, it put the marker very far from where I clicked.
what I am missing?

this is my code:

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

viewer.scene.canvas.addEventListener(‘contextmenu’, (event) => {

event.preventDefault();

const mousePosition = new Cesium.Cartesian2(event.clientX, event.clientY);

const selectedLocation = convertScreenPixelToLocation(mousePosition );

setMarkerInPos(selectedLocation);

}, false);

function convertScreenPixelToLocation(mousePosition) {

const ellipsoid = viewer.scene.globe.ellipsoid;

const cartesian = viewer.camera.pickEllipsoid(mousePosition, ellipsoid);

if (cartesian) {

const cartographic = ellipsoid.cartesianToCartographic(cartesian);

const longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(15);

const latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(15);

return {lat: Number(latitudeString),lng: Number(longitudeString)};

} else {

return null;

}

}

function setMarkerInPos(position){

viewer.pickTranslucentDepth = true;

const locationMarker = viewer.entities.add({

name : ‘location’,

position : Cesium.Cartesian3.fromDegrees(position.lng, position.lat, 300),

point : {

pixelSize : 5,

color : Cesium.Color.RED,

outlineColor : Cesium.Color.WHITE,

outlineWidth : 2

},

label : {

text : ‘check’,

font : ‘14pt monospace’,

style: Cesium.LabelStyle.FILL_AND_OUTLINE,

outlineWidth : 2,

verticalOrigin : Cesium.VerticalOrigin.BOTTOM,

pixelOffset : new Cesium.Cartesian2(0, -9)

}

});

}

‫בתאריך יום ב׳, 11 בפבר׳ 2019 ב-14:41 מאת ‪Omar Shehata‬‏ <‪omar.sameh.shehata@gmail.com‬‏>:‬

viewer.scene.pickPosition is working for me when terrain is on. I’ve modified your example to use it and turn terrain on.

The only other thing I had to change was to set a height reference to be relative to ground:

Sandcastle link.

Working very well, thank you.
there is some viewpoint of the camera that the label of the point suffers from concealment. (as shown in the picture below)
What can I do to handle this problem?
cesiumForSHow.PNG

‫בתאריך יום ג׳, 12 בפבר׳ 2019 ב-18:35 מאת ‪Omar Shehata‬‏ <‪omar.sameh.shehata@gmail.com‬‏>:‬

That’s a good question! This is happening because the terrain geometry is clipping with the label. One way to fix this is to disable depth test on the label:

disableDepthTestDistance : Number.POSITIVE_INFINITY,

``

The clamp to terrain Sandcastle has an example of this:

So the label is always drawn in front of terrain at any view.

working perfectly!
thank you very much

‫בתאריך יום ד׳, 13 בפבר׳ 2019 ב-17:44 מאת ‪Omar Shehata‬‏ <‪omar.sameh.shehata@gmail.com‬‏>:‬

i’ve tried the sandcastle example that omar mentioned above but turns out it doesn’t show the coordinate label, is there any correction to the code from the sandcastle?

1 Like

@Ahyar

Thank you for posting an update - which demo are you looking at?

-Sam