【Cesium】坐标拾取---坐标测量

坐标拾取功能:

1.鼠标左键点击地图,在该处使用红色圆点进行标记;

2.在标记点上方显示经纬度及海拔高度;

3.鼠标右键点击清除所有标记,并取消左键点击标记功能。

 

 代码如下:

/**
 * @description: 获取当前鼠标点击位置坐标
 * @return {*}
 */

function getClickPoint(viewer) {
    // 屏幕点击事件
    var pointsCollection = [];  // 标记点集合,方便后续将标记的点进行清除;
    viewer.screenSpaceEventHandler.setInputAction((evt) => {
        var ray = viewer.camera.getPickRay(evt.position);
        var cartesian = viewer.scene.globe.pick(ray, viewer.scene);
        if(Cesium.defined(cartesian)){
            var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
            // console.log(cartesian, cartographic)
            var lng = Cesium.Math.toDegrees(cartographic.longitude); // 经度值  
            var lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度值  
            var height = cartographic.height;
            var entity = viewer.entities.add({
                position: Cesium.Cartesian3.fromDegrees(lng, lat, height),
                label: { //文字标签
                    text: 'Lon: '+lng.toFixed(5)+'\u00B0'+'\nLat: '+lat.toFixed(5)+'\u00B0'+'\nheight: '+height.toFixed(2)+'m',
                    font: '20px monospace',   //15pt monospace
                    fillColor: Cesium.Color.BLACK, //文字颜色
                    scale: 1,
                    style: Cesium.LabelStyle.FILL,
                    pixelOffset: new Cesium.Cartesian2(-90, -30), //偏移量
                    showBackground: true,
                    backgroundColor: Cesium.Color.AQUA.withAlpha(1),
                    backgroundPadding: new Cesium.Cartesian2(17, 10),
                    horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    disableDepthTestDistance:Number.POSITIVE_INFINITY,
                },
                point:{
                    color:Cesium.Color.RED,
                    pixelSize:9,
                    outlineColor:Cesium.Color.ALICEBLUE,
                    outlineWidth:2,
                    disableDepthTestDistance:Number.POSITIVE_INFINITY,
                },
            })
            pointsCollection.push(entity);
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    viewer.screenSpaceEventHandler.setInputAction(function(){
        // 删除标记点
        pointsCollection.forEach(function (item) {
            if (item instanceof Cesium.Entity) {
                viewer.entities.remove(item);
            }
        })
        // 清除左键点击标记事件
  viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼遇雨愈愉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值