首页 > 代码库 > cesium 检测视图改变的代码

cesium 检测视图改变的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <meta name="description" content="Fly to a specified location or view a geographic rectangle.">
    <meta name="cesium-sandcastle-labels" content="Showcases">
    <title>Cesium Demo</title>
    <script type="text/javascript" src="http://www.mamicode.com/Sandcastle-header.js"></script>
    <script type="text/javascript" src="http://www.mamicode.com/ThirdParty/requirejs-2.1.20/require.js"></script>
    <script type="text/javascript">
    require.config({
        baseUrl : ‘../../../Source‘,
        waitSeconds : 60
    });
    </script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
    @import url(../templates/bucket.css);

    #viewChanged, #cameraChanged {
        display: none;
        background-color: red;
        color: white;
    }
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar">
    <div id="viewChanged">View Changed</div>
    <div id="cameraChanged">Camera Changed</div>
</div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
    ‘use strict‘;
//Sandcastle_Begin
var viewer = new Cesium.Viewer(‘cesiumContainer‘);
var widget = new Cesium.CesiumWidget(‘cesiumContainer‘);
var scene = viewer.scene;
var clock = viewer.clock;
var  timer;

function flyInACity() {
    Sandcastle.declare(flyInACity);
    var camera = scene.camera;
    camera.flyTo({
        destination : Cesium.Cartesian3.fromDegrees(-73.98580932617188, 40.74843406689482, 363.34038727246224),
        complete : function() {
            setTimeout(function() {
                camera.flyTo({
                    destination : Cesium.Cartesian3.fromDegrees(-73.98585975679403, 40.75759944127251, 186.50838555841779),
                    orientation : {
                        heading : Cesium.Math.toRadians(200.0),
                        pitch : Cesium.Math.toRadians(-50.0)
                    },
                    easingFunction : Cesium.EasingFunction.LINEAR_NONE
                });
            }, 1000);
        }
    });
}

Sandcastle.addToolbarMenu([{
    text : ‘Fly in a city‘,
    onselect : function() {
        flyInACity();
        Sandcastle.highlight(flyInACity);
    }
}]);

    /**
     * activates viewchanged Event
     * This event will be fired many times when the camera position or direction is changing
     * @param {Boolean} active
     */

        var camera = scene.camera;
        var posX = camera.position.x;
        var posY = camera.position.y;
        var posZ = camera.position.z;
        var dirX = camera.direction.x;
        var dirY = camera.direction.y;
        var dirZ = camera.direction.z;

        // tolerance
        var posD = 3;
        var dirD = 0.001;

        var listenerFunc = function() {
            var currentCamera = scene.camera;
            var _posX = currentCamera.position.x;
            var _posY = currentCamera.position.y;
            var _posZ = currentCamera.position.z;
            var _dirX = currentCamera.direction.x;
            var _dirY = currentCamera.direction.y;
            var _dirZ = currentCamera.direction.z;


            if (Math.abs(posX - _posX) > posD ||
                Math.abs(posY - _posY) > posD ||
                Math.abs(posZ - _posZ) > posD ||
                Math.abs(dirX - _dirX) > dirD ||
                Math.abs(dirY - _dirY) > dirD ||
                Math.abs(dirZ - _dirZ) > dirD) {
                posX = _posX;
                posY = _posY;
                posZ = _posZ;
                dirX = _dirX;
                dirY = _dirY;
                dirZ = _dirZ;

                if (timer != null) {
                    clearTimeout(timer);
                }
                timer = setTimeout(function(){
                    console.log("Wake up Tiling Manager from sleep");
                    timer = null;
                }, 100 + 100*Math.random());
            }
        };


    widget.clock.onTick.addEventListener(listenerFunc);




//Sandcastle_End
    Sandcastle.finishedLoading();
}
if (typeof Cesium !== "undefined") {
    startup(Cesium);
} else if (typeof require === "function") {
    require(["Cesium"], startup);
}
</script>
</body>
</html>

 

cesium 检测视图改变的代码