运行范例

描述

范例展示如何使用 TrackingLayer 上的 hitTest 方法来进行模型对象的查询。

解微软脚本库的更多信息请参见微软官方帮助文档微软官方帮助文档.

代码 (文件格式为 UTF-8)


<html>
<head>
    <meta http-equiv="x-ua-compatible" content="IE=8"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>获取跟踪图层feature3d</title>
    <script type="text/javascript" src="SuperMap.Include.js"></script>
    <script type="text/javascript">
        var sceneControl = null;
        var scene = null;
        var layer3Ds = null;
        var screenLayer = null;
        var trackingLayer = null;
        var asyncHelper = null;

        var htmlUrl = document.location.host;
        //判断网页的打开方式是本地打开还是通过网络打开
        //不同的打开方式url赋值不同
        if (htmlUrl == "") {
            htmlUrl = "http://localhost:8090";
        }
        else {
            htmlUrl = "http://" + htmlUrl;
        }
        var sceneUrl = htmlUrl + "/iserver/services/3D-sample/rest/realspace";

        function onPageLoad() {
            //初始化三维场景控件
            try {
                //初始化三维场景控件实例,参数为包含控件的HTML元素
                sceneControl = new SuperMap.Web.UI.Controls.SceneControl($get("sceneControlDiv"), initCallback, failedCallback);
            }
            catch (e) {
                //若没有安装插件,则抛出该异常
                if (e.name == SuperMap.Web.Realspace.ExceptionName.PlugInNotInstalled) {
                    var url = htmlUrl + "/iserver/iClient/for3D/plugin/Setup.exe";
                    document.write("<a href='" + url + "'>未检测到 SuperMap iClient3D for  Plugin 插件,请单击此处下载并安装插件。</a>");
                    return;
                }
                //若使用非IE浏览器,则抛出该异常
                else if (e.name == SuperMap.Web.Realspace.ExceptionName.BrowserNotSupport) {
                    document.write("<p>SuperMap iClient3D for  Plugin 目前仅支持 InternetExplorer 浏览器,请更换浏览器后重新尝试加载本页面。</p>");
                    return;
                }
                //抛出其他异常
                else {
                    alert(e.message);
                }
            }
        }

        //控件初始化完成后的回调函数,初始化完成之后才能进行数据加载 			
        function initCallback() {

            scene = sceneControl.get_scene();

            screenLayer = scene.get_screenLayer3D();

            trackingLayer = scene.get_trackingLayer3D();

            screenLayer.set_isVisible(true);

            trackingLayer.set_isVisible(true);

            asyncHelper = sceneControl.get_asyncHelper();
        }

        //控件初始化失败后的回调函数
        function failedCallback() {
            alert("Realspace initialized failed!");
        }




        //跟踪层加载地物	
        function add() {

            jeepModel = new SuperMap.Web.Core.GeoModel();
            var modelurl = htmlUrl + "/RealspaceSample/jeep.zip";
            jeepModel.fromModelFile(modelurl);
            jeepModel.set_position(new SuperMap.Web.Core.Point3D(116.3875041600627, 39.989431841407026, 6.50));
            var style = new SuperMap.Web.Core.Style3D();
            style.set_altitudeMode(SuperMap.Web.Realspace.AltitudeMode.RELATIVE_TO_GROUND);
            style.set_bottomAltitude("3.5");
            var feature = new SuperMap.Web.Core.Feature3D();
            feature.set_geometry(jeepModel);
            feature.set_style3D(style);
            trackingLayer.add(feature, "jeep");
            var camera = new SuperMap.Web.Realspace.Camera();
            camera.set_longitude(116.3875041600627);
            camera.set_latitude(39.989431841407026 + 0.0002);
            camera.set_altitude(40);
            camera.set_tilt(75);
            scene.get_flyingOperator().flyTo(camera);
            }
           

        }
        function getInfo() {
            //设置控件的当前操作为自定义操作
            var myAction = new SuperMap.Web.UI.Action3Ds.MyAction(sceneControl);
            sceneControl.set_sceneAction(myAction);
        }

        //通过继承SceneAction的方式来扩展用户的Action,继承方式采用框架提供的格式
        //扩展Action的构造函数
        SuperMap.Web.UI.Action3Ds.MyAction = function (sceneControl) {
            ///<param name="sceneControl" type="SuperMap.Web.UI.Controls.SceneControl"></param>
            SuperMap.Web.UI.Action3Ds.MyAction.initializeBase(this);

            this._name = "myAction";

            this._sceneControl = sceneControl;

            //对应的场景操作类型为漫游
            this._type = SuperMap.Web.UI.Action3Ds.SceneActionType.POINTSELECT;

        };

        //扩展Action的属性和方法
        SuperMap.Web.UI.Action3Ds.MyAction.prototype = {
            /*
            * 析构方法
            */
            dispose: function () {
                ///<returns type="void"></returns>
                this._sceneControl = null;
            },

            /*
            * 鼠标单击方法
            */
            onMouseDown: function (e) {

                //通过事件对象查询到相关的信息

                var x = e.get_clientX();
                var y = e.get_clientY();
                var point = new SuperMap.Web.Core.Point(x, y);

                var select = scene.get_trackingLayer3D().hitTest(point);
                if (select == null)
                { alert("你没有选中对象") }
                else
                { alert("你选中了对象," + "对象名字为:" + select.get_name()) }


            }
        };
        SuperMap.Web.UI.Action3Ds.MyAction.registerClass('SuperMap.Web.UI.Action3Ds.MyAction', SuperMap.Web.UI.Action3Ds.SceneAction, Sys.IDisposable);

        function setPan() {
            //设置控件的当前操作为漫游
            var panAction = new SuperMap.Web.UI.Action3Ds.Pan(sceneControl);

            sceneControl.set_sceneAction(panAction);
        }
			

    </script>
</head>
<body onload="onPageLoad()" style="width: 800px; height: 500px">
    <div id="toolSet" style="position:absolute;top:5px;height: 25px; width: 100%; float: left">
        <input id="fetchModel" type="button" value="加载 模型" onclick="add()" />
        <input id="fetchModel1" type="button" value="获取Feature3D" onclick="getInfo()" />
        <input id="button" type="button" value=" 漫    游 " onclick="setPan()" />
    </div>
    <div id="sceneControlDiv" style="position:absolute;top: 30px;bottom: 5px;left: 5px;right: 5px;height: 99%; width: 100%;">
    </div>
</body>
</html>