运行范例

描述

该范例展示如何加载通过SuperMap iServer 8C 发布的影像、模型、kml、kmz、地形图层

代码 (文件格式为 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 src="scripts/jquery.tools.min.js"></script>
		<script type="text/javascript" src="scripts/samplecode.js"></script>
		<link href="style/index.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="SuperMap.Include.js" ></script>
		<script type="text/javascript">
			var scene = null;
			var sceneControl = null;
			var layer3Ds = null;
			var terrainLayers = null;
			var serverAddress = null;
			var htmlUrl = document.location.host;
			var layer3DServicesList = null;
			//判断网页的打开方式是本地打开还是通过网络打开
			//不同的打开方式url赋值不同
			if(htmlUrl == "")
			{
				htmlUrl = "http://localhost:8090";
			}
			else
			{
				htmlUrl = "http://" + htmlUrl;
			}
			var sceneUrl = htmlUrl + "/iserver/services/3D-sample/rest/realspace";
            var mapurl=htmlUrl + "/iserver/services/map-world/rest/maps/World"
			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();

				//iServer服务地址
				serverAddress = sceneUrl;
				
				//获取三维图层信息列表对象实例
				layer3DServicesList = sceneControl.get_layer3DServicesList();

				//获取三维图层集合 
				layer3Ds = scene.get_layer3Ds();

				//获取地形图层集合
				terrainLayers = scene.get_terrainLayers();
				
				//从指定的服务器获取场景的图层服务信息,获取成功返回 True
				var bLoad = layer3DServicesList.load(serverAddress, "scene");
				if(!bLoad)
				{
					alert("获取图层服务信息失败!");
				}
			}

			//控件初始化失败后的回调函数
			function failedCallback() 
			{
				alert("Realspace initialized failed!");
			}
		
			//往场景中添加指定图层
			function loadLayer(addLayerName)
			{
					//获取指定的图层信息,包括图层名称、数据名称、图层类型
					var layer3DServiceInfo = layer3DServicesList.get_item(addLayerName);
					var layerName = layer3DServiceInfo.get_name();
					var dataName = layer3DServiceInfo.get_dataName();
					var layer3DType = layer3DServiceInfo.get_type();
					//如果为地形图层,则使用 TerrainLayers 加载,如果为模型、KML或者矢量图层则使用 Layer3Ds 加载
					if(layer3DType == SuperMap.Web.Realspace.Layer3DType.TERRAIN)
					{
						terrainLayers.add(serverAddress, layerName, dataName, false);
						var geobound = terrainLayers.get_item(layerName).get_bounds();
						scene.get_flyingOperator().flyToBounds(geobound);
					}
					else
					{
						layer3Ds.add(serverAddress, layerName, dataName, layer3DType, false);
						var geobound = layer3Ds.get_item(layerName).get_bounds();
						scene.get_flyingOperator().flyToBounds(geobound);
					}
			}
					
			//移除场景中的图层
			function removeLayers()
			{
				layer3Ds.removeAll();
				terrainLayers.removeAll();
			}
    function addRestMap(){

    var maplayer=layer3Ds.add(mapurl, "World", "World", SuperMap.Web.Realspace.Layer3DType.Map);


    }
		</script>
	</head>

	<body onLoad="onPageLoad()"> 
		<div id="LoadLayersToolSet" style="position:absolute;left:5px;top:0px;width:220px;bottom:0px"> 
		<div id="slide">
		<ul id="menu">
		<li>
		<a href="#" >图层加载</a>
		<ul>
		<li><a href="#" onClick="loadLayer('Building@OlympicGreen')">矢量模型图层</a></li>						
		<li><a href="#" onClick="loadLayer('beijing@beijing')">影像图层</a></li>
		<li><a href="#" onClick="loadLayer('2015beijingsubway')">KMZ图层</a></li>
		<li><a href="#" onClick="loadLayer('bridge_beijing')"> KML图层 </a></li>
		<li><a href="#" onClick="loadLayer('Beijing@RealspaceSample_Terrain')">地形图层</a></li>
		<li><a href="#" onClick="removeLayers()">移除图层</a></li>                       
		</ul>
		</li>
		</ul>
		</div>
		</div> 
		<div id="sceneControlDiv"style="position:absolute;left:230px;top:5px;bottom:5px;right:5px">
		</div>	
	</body>
 </html>