运行范例

描述

该范例展示了通过设置当前场景的地下信息(包括地下的深度等)及设置地表信息(包括透明度、设置挖空区域等)来表达模型在场景中地下的显示效果,实现了地上地下一体化的表达和浏览。

代码 (文件格式为 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>地下三维</title>
		<script type="text/javascript" src="SuperMap.Include.js" ></script>
		<script type="text/javascript">
		var htmlUrl = document.location.host;
		var scene = null;
		var sceneControl = null;
		var underground = null;
		var globalImage = null;
		var excavationRegionValue = 0;

		//判断网页的打开方式是本地打开还是通过网络打开
		//不同的打开方式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();
				
			//设置地下深度等信息
			underground = scene.get_underground();
			underground.set_isVisible(true);
			underground.set_depth(1000);
				
			//设置地表透明度信息,值域范围在0-1
			globalImage = scene.get_globalImage();
			globalImage.set_transparency(document.getElementById("TransparencyValue").value);

			//在场景中添加图层并设置图层的显示风格
			var layer3D = scene.get_layer3Ds().add(sceneUrl, "Underground", getDataName("Underground"), SuperMap.Web.Realspace.Layer3DType.VECTOR);

			if (layer3D == null) 
			{
				alert("获取图层失败!");
			}
			
				
			//获取图层的地理范围,并飞行到该范围
			var geobound = layer3D.get_bounds();   
			scene.get_flyingOperator().flyToBounds(geobound);
		}

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

		//获取数据信息
		function getDataName(strLayerName) 
		{
			var sceneservicelist = sceneControl .get_sceneServicesList();
			sceneservicelist.load(sceneUrl);
			var scenecount = sceneControl .get_sceneServicesList().get_count();

			for (var i = 0; i < scenecount; i++) 
			{
				//获取指定的场景信息
				var sceneinfor = sceneservicelist.get_item(i);
				var scenename = sceneinfor.get_name();

				layer3DServicesList = sceneControl .get_layer3DServicesList();
				layer3DServicesList.load(sceneUrl, scenename);

				var count = layer3DServicesList.get_count();
				for (var j = 0; j < count; j++) 
				{
					//获取指定的图层信息,包括图层名称、数据名称
					var layer3DServiceInfo = layer3DServicesList.get_item(j);
					var layerName = layer3DServiceInfo.get_name();
					var dataName = layer3DServiceInfo.get_dataName();
					if (layerName == strLayerName) 
					{
						return dataName;
					}
				}
			}
		}

		function measureArea()
		{
			var measureAreaAction = new SuperMap.Web.UI.Action3Ds.ExcavationRegion(sceneControl);
			sceneControl.set_sceneAction(measureAreaAction); 
				
			sceneControl.addEvent("addExcavationRegion", areaFinishedHandler);
		}

		function areaFinishedHandler(region3D)
		{
			globalImage.addExcavationRegion(region3D,excavationRegionValue++);
		}

		//设置透明度
		function setTransparency()
		{
			var TransparencyValue = document.getElementById("TransparencyValue").value;
			globalImage.set_transparency(TransparencyValue); 
		}

		//清除挖空区域
		function cleanExcavationRegion()
		{
			globalImage.clearExcavationRegions();

			excavationRegionValue = 0;

			sceneControl.set_sceneAction(new SuperMap.Web.UI.Action3Ds.Pan(sceneControl));
		}

		//暂停挖空区域
		function stopMeasureArea()
		{
			sceneControl.set_sceneAction(new SuperMap.Web.UI.Action3Ds.Pan(sceneControl));
		}
		</script>
	</head>

	<body onLoad="onPageLoad()">  	
		<div id="flyWindow" style="position:absolute;left:5px;top:5px;bottom:5px;right:5px;float:left;">
			透明度(0~1):<input id="transparencyValue" type="text" value="1"/>
			<input id="TransparencyValue" type="button" value="设置" onclick="setTransparency()" />
			<input id="measureArea" type="button" value="设置挖空区域" onclick="measureArea()" />
			<input id="stopMeasureArea" type="button" value="暂停挖空区域" onclick="stopMeasureArea()" />
			<input id="cleanExcavationRegion" type="button" value="清除挖空区域" onclick="cleanExcavationRegion()" />
		</div>
		<div id="sceneControlDiv" style="position:absolute;left:5px;top:35px;bottom:5px;right:5px"> 
		</div>
	</body>
</html>