运行范例

描述

该范例展示如何通过用户自定义扩展的Action来进行地标的标绘。示例中通过微软脚本库来继承SceneAction类。

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

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


    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset = UTF-8" />
        <meta http-equiv="x-ua-compatible" content="IE=8"/>
        <title>DrawPlacemarkAction</title>
        <script type="text/javascript" src="SuperMap.Include.js"></script>
        <script type="text/javascript">
            var sceneControl = null;
            var scene = null;
            var htmlUrl = document.location.host;
            //判断网页的打开方式是本地打开还是通过网络打开
            //不同的打开方式url赋值不同
            if (htmlUrl == "") {
                htmlUrl = "http://localhost:8090";
            }
            else {
                htmlUrl = "http://" + htmlUrl;
            }

            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() {
                //获取Realspace控件的场景,控件和场景是一对一的捆绑关系
                scene = sceneControl.get_scene();
            }

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

            function pan() {
                //设置当前Action为pan  
                var panAction = new SuperMap.Web.UI.Action3Ds.Pan(sceneControl);
                sceneControl.set_sceneAction(panAction);
            }

            function drawPlacemark() {
                //设置当前Action为自定义的地标绘制
                var myAction = new SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction(sceneControl);
                sceneControl.set_sceneAction(myAction);
            }

            //继承SuperMap.Web.UI.Action3Ds.SceneAction类扩展一个SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction
            //该类用于标绘地标
            SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction = function (sceneControl) {
                SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction.initializeBase(this);

                this._name = "DrawPlacemark";

                this._sceneControl = sceneControl;

                this._type = SuperMap.Web.UI.Action3Ds.SceneActionType.PAN;

            };

            SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction.prototype = {
                /*
                * 方法
                */
                dispose: function () {
                    this._sceneControl = null;
                },

                /*
                * 方法
                */

                onMouseDown: function (e) {
                    //创建placemark中的feature3D
                    var geometry = new SuperMap.Web.Core.GeoPoint3D(new SuperMap.Web.Core.Point3D(e.get_longitude(), e.get_latitude(), e.get_altitude()));
                    var feature3D = new SuperMap.Web.Core.Feature3D();

                    //设置feature3D中的style3D属性
                    var style = new SuperMap.Web.Core.Style3D();
                    var pictureurl = htmlUrl + "/RealspaceSample/iClient.png";
                    style.set_markerFile(pictureurl);


                    feature3D.set_style3D(style);
                    feature3D.set_geometry(geometry);

                    //创建Geoplacemark
                    var geoPlacemark = new SuperMap.Web.Core.GeoPlacemark();
                    geoPlacemark.set_feature3D(feature3D);
                    geoPlacemark.set_name("my place");

                    //创建几何对象为Geoplacemark的feature3D,并加入跟踪图层中
                    var placemarkfeature3D = new SuperMap.Web.Core.Feature3D();
                    placemarkfeature3D.set_geometry(geoPlacemark);

                    var trackingLayer = scene.get_trackingLayer3D();
                    //跟踪图层设置为可见
                    trackingLayer.set_isVisible(true);
                    trackingLayer.add(placemarkfeature3D, "hello");

                }
            };
            SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction.registerClass('SuperMap.Web.UI.Action3Ds.DrawPlacemarkAction', SuperMap.Web.UI.Action3Ds.SceneAction, Sys.IDisposable);
  
        </script>
    </head>
    <body onload="onPageLoad()">
        <div id="toolSet" style="position:absolute;top: 5px ;height:25px;float: left">
            <input id="" type="button" value="绘制地标" onclick="drawPlacemark()">
            <input id="Pan" type="button" value="漫游" onclick="pan()">
        </div>
        <div iid="sceneControlDiv" style="position:absolute;top:30px;bottom: 5px;left: 5px;right: 5px;">
        </div>
    </body>
    </html>