Add a popup after user click on entity

Hi,

Some examples on this forum and on the internet are explaining how to add a HTML popup (in my case a menu) on the top of an entity after a user has clicked on it.

My html popup element is positioning well on the first click (image 1), but as soon as I’m moving on the map the HTML element does not update its position correctly (image 2).

Image 1
The menu is well positioning below the entity

Image 2
The position is not correct anymore

Here is my code :
First on left click I get pickedFeature and store the result in this.viewer.selectedEntity.
Then after the camera moveEnd event I display the menu using the selectedEntity position.
This code works fine on the first pick event but if I move the wgs84ToWindowCoordinates are not correct anymore.

this.viewer.screenSpaceEventHandler.setInputAction((click) => {
            var pickedFeature = this.viewer.scene.pick(click.position);
            this.viewer.selectedEntity = pickedFeature.id;
            if (Cesium.defined(pickedFeature)){
                this.viewer.flyTo(
                    pickedFeature.id, {
                        offset: new Cesium.HeadingPitchRange(0, Cesium.Math.toRadians(-90), 0),
                    }
                );
            }            
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

this.viewer.camera.moveEnd.addEventListener(() => {
            if(this.viewer.selectedEntity){
                console.log(" ***************** moveEnd ***************** ");
                var screenCoordinates = Cesium.SceneTransforms.wgs84ToWindowCoordinates(this.viewer.scene, this.viewer.selectedEntity.position._value);  
                console.log(screenCoordinates);
                const top = `${Math.round(screenCoordinates.y)}px`;
                const left = `${Math.round(screenCoordinates.x)-150}px`;
                document.getElementById("menu-tooltip").style.display = 'block';
                document.getElementById("menu-tooltip").style.top = top;
                document.getElementById("menu-tooltip").style.left = left;
                console.log(" ***************** moveEnd ***************** ");
            }
        })  

Do you see what I’m doing wrong ?

Thanks for your help.

1 Like

After a few tests, I noticed that if I click on my entity and I’m at a high altitude, the popup position moves well. But if I zoom in on the entity, that’s when the positioning becomes aberrant.

Is it because that I’m using a 3D map ? Should the screen coordinates position compute in another way ?