mirror of
				https://github.com/jiawanlong/Cesium-Examples.git
				synced 2025-11-04 01:04:17 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			193 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			193 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<!--********************************************************************
 | 
						|
* by jiawanlong
 | 
						|
*********************************************************************-->
 | 
						|
<!DOCTYPE html>
 | 
						|
<html>
 | 
						|
 | 
						|
<head>
 | 
						|
    <meta charset="UTF-8" />
 | 
						|
    <link rel="stylesheet" href="./../../libs/cesium/Cesium1.98/Widgets/widgets.css">
 | 
						|
    <script type="text/javascript" src="./../../libs/cesium/Cesium1.98/Cesium.js"></script>
 | 
						|
 | 
						|
</head>
 | 
						|
 | 
						|
<body style="margin: 0; overflow: hidden; background: #fff; width: 100%; height: 100%; position: absolute; top: 0">
 | 
						|
    <div id="map" style="margin: 0 auto; width: 100%; height: 100%"></div>
 | 
						|
    <script type="text/javascript">
 | 
						|
 | 
						|
        Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIwNDljNWFmZC03MzRlLTRiMDMtYWIwMi00Yjk4YWQ4NzQwZGEiLCJpZCI6MjU5LCJpYXQiOjE3NTEzNzkyMzR9.OTqPNs3UGNnT1LYkPTavV80wN8Es_YphpJgQcpdnqWc'
 | 
						|
        const viewer = new Cesium.Viewer('map', {
 | 
						|
            imageryProvider: false,
 | 
						|
            baseLayerPicker: false,
 | 
						|
        });
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        var xyz = new Cesium.UrlTemplateImageryProvider({
 | 
						|
            "credit": "mapbox",
 | 
						|
            "url": 'https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.webp?sku=1016Ab1dNMw2X&access_token=pk.eyJ1IjoidHJhbXBqd2wiLCJhIjoiY2xhYXIxbHExMDN3dzN3cGliOHdrMThxMiJ9.6er2aYb1EBjSsK1-t9d2-w'
 | 
						|
        })
 | 
						|
        viewer.imageryLayers.addImageryProvider(xyz)
 | 
						|
 | 
						|
        // Primitive
 | 
						|
        //     1、GeometryInstance
 | 
						|
        //     2、Apperance
 | 
						|
        //     3、modelMatrix
 | 
						|
 | 
						|
 | 
						|
        // 开启帧率
 | 
						|
        viewer.scene.debugShowFramesPerSecond = true;
 | 
						|
        function showPoint(x, y, z, p) {
 | 
						|
            viewer.entities.add({
 | 
						|
                position: Cesium.Cartesian3.fromDegrees(x, y, z),
 | 
						|
                point: {
 | 
						|
                    pixelSize: 5,
 | 
						|
                    color: Cesium.Color.RED,
 | 
						|
                    outlineColor: Cesium.Color.WHITE,
 | 
						|
                    outlineWidth: 2,
 | 
						|
                },
 | 
						|
                id: '' + p,
 | 
						|
                label: {
 | 
						|
                    text: p,
 | 
						|
                    font: '14pt monospace',
 | 
						|
                    outlineWidth: 2,
 | 
						|
                    horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
 | 
						|
                    pixelOffset: new Cesium.Cartesian2(10, 0),
 | 
						|
                }
 | 
						|
            })
 | 
						|
        }
 | 
						|
        showPoint(121.41777777777779, 31.233333333333334, -1961.6396551739094, '111')
 | 
						|
 | 
						|
        showPoint(121.41777777777779, 31.242222222222225, -1961.6396551739094, '2')
 | 
						|
        showPoint(121.41777777777779, 31.242222222222225, -980.8952536478324, '3')
 | 
						|
        showPoint(121.41777777777779, 31.233333333333334, -980.8952536478324, '4')
 | 
						|
 | 
						|
 | 
						|
        // 多边形
 | 
						|
        var polygonInstance = new Cesium.GeometryInstance({
 | 
						|
            id: {
 | 
						|
                "name": 'polygon'
 | 
						|
            },
 | 
						|
            geometry: new Cesium.PolygonGeometry({
 | 
						|
 | 
						|
                polygonHierarchy: new Cesium.PolygonHierarchy(
 | 
						|
                    Cesium.Cartesian3.fromDegreesArrayHeights([
 | 
						|
                        121.41777777777779, 31.233333333333334, -1961.6396551739094,
 | 
						|
                        121.41777777777779, 31.242222222222225, -1961.6396551739094,
 | 
						|
                        121.41777777777779, 31.242222222222225, -980.8952536478324,
 | 
						|
                        121.41777777777779, 31.233333333333334, -980.8952536478324,
 | 
						|
 | 
						|
                    ])
 | 
						|
                ),
 | 
						|
                perPositionHeight: true,
 | 
						|
                // extrudedHeight:2000
 | 
						|
            }),
 | 
						|
 | 
						|
        });
 | 
						|
        viewer.scene.primitives.add(new Cesium.Primitive({
 | 
						|
            geometryInstances: [polygonInstance],
 | 
						|
            appearance: new Cesium.MaterialAppearance({
 | 
						|
                material: Cesium.Material.fromType('Color', {
 | 
						|
                    color: Cesium.Color.CYAN.withAlpha(1),
 | 
						|
                })
 | 
						|
            })
 | 
						|
        }));
 | 
						|
 | 
						|
 | 
						|
        
 | 
						|
        const wall = new Cesium.WallGeometry({
 | 
						|
            positions: Cesium.Cartesian3.fromDegreesArrayHeights([
 | 
						|
                121.41777777777779, 31.233333333333334, -1961.6396551739094,
 | 
						|
                121.41777777777779, 31.242222222222225, -1961.6396551739094,
 | 
						|
                121.41777777777779, 31.242222222222225, -980.8952536478324,
 | 
						|
                121.41777777777779, 31.233333333333334, -980.8952536478324,
 | 
						|
            ])
 | 
						|
        });
 | 
						|
        const geometry = Cesium.WallGeometry.createGeometry(wall);
 | 
						|
 | 
						|
        var polygonInstance = new Cesium.GeometryInstance({
 | 
						|
            id: {
 | 
						|
                "name": 'polygon'
 | 
						|
            },
 | 
						|
            geometry: geometry,
 | 
						|
 | 
						|
        });
 | 
						|
        viewer.scene.primitives.add(new Cesium.Primitive({
 | 
						|
            geometryInstances: [polygonInstance],
 | 
						|
            appearance: new Cesium.MaterialAppearance({
 | 
						|
                material: Cesium.Material.fromType('Color', {
 | 
						|
                    color: Cesium.Color.CYAN.withAlpha(1),
 | 
						|
                })
 | 
						|
            })
 | 
						|
        }));
 | 
						|
 | 
						|
        // 线
 | 
						|
        var lineInstance = new Cesium.GeometryInstance({
 | 
						|
            id: {
 | 
						|
                "name": 'line'
 | 
						|
            },
 | 
						|
            geometry: new Cesium.PolylineGeometry({
 | 
						|
                positions: Cesium.Cartesian3.fromDegreesArray([
 | 
						|
                    -100, 40,
 | 
						|
                    -90, 40,
 | 
						|
                    -120, 60,
 | 
						|
                ]),
 | 
						|
                width: 15,
 | 
						|
                vertexFormat: Cesium.PolylineColorAppearance.VERTEX_FORMAT,
 | 
						|
            }),
 | 
						|
            attributes: {
 | 
						|
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString("#001aff").withAlpha(0.5))
 | 
						|
            }
 | 
						|
        })
 | 
						|
        viewer.scene.primitives.add(new Cesium.Primitive({
 | 
						|
            geometryInstances: [lineInstance],
 | 
						|
            appearance: new Cesium.PolylineColorAppearance()
 | 
						|
        }))
 | 
						|
 | 
						|
        // 发光的线
 | 
						|
        viewer.scene.primitives.add(new Cesium.Primitive({
 | 
						|
            geometryInstances: new Cesium.GeometryInstance({
 | 
						|
                geometry: new Cesium.PolylineGeometry({
 | 
						|
                    positions: Cesium.Cartesian3.fromDegreesArray([
 | 
						|
                        -110, 40,
 | 
						|
                        -100, 40,
 | 
						|
                        -130, 60,
 | 
						|
                    ]),
 | 
						|
                    width: 20.0,
 | 
						|
                    vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
 | 
						|
                }),
 | 
						|
                id: {
 | 
						|
                    "name": '发光的线'
 | 
						|
                },
 | 
						|
            }),
 | 
						|
            appearance: new Cesium.PolylineMaterialAppearance({
 | 
						|
                material: Cesium.Material.fromType(Cesium.Material.PolylineGlowType, {
 | 
						|
                    color: new Cesium.Color(1.0, 0.0, 0.0, 1.0)
 | 
						|
                })
 | 
						|
            })
 | 
						|
        }));
 | 
						|
 | 
						|
        // -------Primitive点击事件-------------
 | 
						|
        var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
 | 
						|
        handler.setInputAction(function (clickEvent) {
 | 
						|
            var ray1 = viewer.camera.getPickRay(clickEvent.position);
 | 
						|
            var cartesian = viewer.scene.globe.pick(ray1, viewer.scene);
 | 
						|
            var pick = viewer.scene.pickPosition(clickEvent.position);
 | 
						|
            var pickEd = viewer.scene.pick(clickEvent.position);
 | 
						|
            if (pickEd && pick) {
 | 
						|
                console.log(pickEd.id)
 | 
						|
            }
 | 
						|
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
 | 
						|
        var flag = false;
 | 
						|
        var models = viewer.scene.primitives._primitives;
 | 
						|
        for (let i = 0; i < models.length; i++) {
 | 
						|
            if (models[i]._url) {
 | 
						|
                flag = true;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        console.log(flag)
 | 
						|
 | 
						|
    </script>
 | 
						|
</body>
 | 
						|
 | 
						|
</html> |