运行范例

描述

该范例展示如何加载通过SuperMap iServer 8C 发布的场景服务,包括加载场景与关闭场景功能。

代码 (文件格式为 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 iClient 7C for Realspace目前只支持IE浏览器</p>");
						return;
					}
					//抛出其他异常
					else
					{
						alert(e.message);
					}
				}
			}

				//控件初始化完成后的回调函数,初始化完成之后才能进行数据加载
				function initCallback() 
				{
					//获取地球控件场景,控件和场景是一对一的捆绑关系
					scene = sceneControl.get_scene();
				}

			//控件初始化失败后的回调函数
			function failedCallback() 
			{
				alert("Realspace initialized failed!");
			}
			
			function SceneOpen_onclick()
			{
				//iServer服务地址
				var sceneAddress = document.getElementById("ServerAddress").value;
				
				//场景名称
				var sceneName = document.getElementById("SceneName").value;
			
				//打开场景
				scene.open(sceneAddress,sceneName);
				
				//飞行到场景的第1个图层的地理范围
				var layer3D = scene.get_layer3Ds().get_item(0);
                if (layer == null)
				{
					layer = scene.get_terrainLayers().get_item(0);
				}

				var geobound = layer3D.get_bounds();   
					
				scene.get_flyingOperator().flyToBounds(geobound);
				
				//给场景重命名
				scene.set_name("hello");
			}  
			
			function SceneClose_onclick()
			{
				//关闭场景
				scene.close();
				scene.refresh();
			}  
		</script>
	</head>

	<body onLoad="onPageLoad()"> 		
		<div style=position:absolute;left:5px;top:5px;right:5px;float:left >
		<!--服务器-->
		服务器地址:
		<input id="ServerAddress" name="ServerAddress" type="text" value="http://localhost:8090/iserver/services/components-rest/rest/realspace"" />
		   
		<!--场景--> 
		场景服务:
		<input id="SceneName" name="SceneName" type="text" value="scene" /> 
		<input id="SceneOpen" type="button" value="加载场景" onClick="return SceneOpen_onclick()" /> 
		<input id="SceneClose" type="button" value="删除场景" onClick="return SceneClose_onclick()" /> <br /> 
		</div> 
		<div id="sceneControlDiv" style="position:absolute;left:5px;top:35px;bottom:5px;right:5px"> 
		</div> 
	</body> 

</html>