运行范例

描述

该范例展示如何设置场景的Action来操作场景,包括对象的选择以及漫游操作,并且展示了场景的全局显示以及刷新功能

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


<html>				
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=8" />	
		<meta http-equiv="Content-Type" content="text/html; UTF-8" />
		<title>基本操作</title>
		<script type="text/javascript" src="SuperMap.Include.js" ></script>
		<script type="text/javascript"<
			var scene = null;
			var sceneControl = 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()
			{
				//获取Realspace控件的场景,控件和场景是一对一的捆绑关系
				scene = sceneControl.get_scene();
				//加载场景
				SceneLoad();
				//给选择事件注册回调函数,选择事件返回当前有选中对象的所有选择集,类型是选择集的数组
				sceneControl.addEvent("objectSelected", handler);
			}
			//控件初始化失败后的回调函数
			function failedCallback()
			{
				alert("Realspace initialized failed!");
			}	
			//选择事件的回调函数,弹出选中对象的id
			function handler(selection3Ds) 
			{
				//获取选择集的数目
				var selection3DCount =  selection3Ds.length;
				for(var i=0; i<selection3DCount; i++)
				{
					//获取选择集中被选中对象的数目
					var selectCount = selection3Ds[i].get_count();
					for(var j=0; j<selectCount; j++)
					{
						//获取选择集
						var selection3D = selection3Ds[i];
						var layer3D = scene.get_layer3Ds().get_item(selection3D.get_layer3D().get_name());
						var selectObjectName = layer3D.findFeature3DByID(selection3D.get_item(j)).get_name();
						alert(selection3D.get_item(j)+"\n"+selectObjectName);
					}
				}
			}
			function SceneLoad()
			{
				//指定场景的服务器地址
				var sceneAddress = sceneUrl;
			
				//指定图层名称
				var sceneName = "scene";
			
				//打开场景
				scene.open(sceneAddress, sceneName);
				//获取场景中的图层
				var layer3D = scene.get_layer3Ds().get_item("Ground@OlympicGreen"); 			
				if (layer3D != null)
				{
					//获取图层的地理范围,并飞行到该范围
					var geobound = layer3D.get_bounds();  
					scene.get_flyingOperator().flyToBounds(geobound);
				}	
			}
			function viewEntire()
			{
				//全局显示整个三维场景
				scene.viewEntire();
			}
			function setPan()
			{
				//设置控件的当前操作为漫游
				var panAction = new SuperMap.Web.UI.Action3Ds.Pan(sceneControl);
				sceneControl.set_sceneAction(panAction);
			}
			function setSelect()
			{
				//设置控件的当前操作为选择
				var selectAction = new SuperMap.Web.UI.Action3Ds.Select(sceneControl);
				sceneControl.set_sceneAction(selectAction); 
			}
			function refresh()
			{   
				//刷新场景
				scene.refresh();
			}
		</script>

	</head>
	
	<body onLoad="onPageLoad()">
		<div id="toolSet" style="position:absolute;top:5px;float:left;">
			<!--全幅显示-->
			<input id="viewentire" type="button" value="全幅显示" onClick="return viewEntire()" />
			<!--漫游-->
			<input id="pan" type="button" value="漫    游" onClick="return setPan()" />
			<!--选择-->
			<input id="select" type="button" value="选    择" onClick="return setSelect()" />
			<!--刷新-->
			<input id="refresh" type="button" value="刷    新" onClick="return refresh()" />
		</div>
		<div id="sceneControlDiv" style="position:absolute;top:35px;bottom:5px;left:5px;right:5px"> 
		</div>
	</body>
</html>