运行范例

描述

范例展示如何获取矢量图层选择集中元素的属性字段信息。

代码 (文件格式为 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>属性字段查询</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;
			}
			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();

				//指定场景的服务器地址
				var sceneAddress = sceneUrl;

				//指定图层名字
				var layerName = "buildings@RealspaceSample";

				//获取三维图层信息列表对象实例
				var layer3DServicesList = sceneControl.get_layer3DServicesList();
					
				//从指定的服务器获取场景的图层服务信息,获取成功返回 True
				var bLoad = layer3DServicesList.load(sceneAddress, "scene");
				if(bLoad)
				{
					var layer3DServiceInfo = layer3DServicesList.get_item(layerName);
					var dataName = layer3DServiceInfo.get_dataName();			
					//打开图层
					var layer3D = scene.get_layer3Ds().add(sceneAddress, layerName, dataName, SuperMap.Web.Realspace.Layer3DType.VECTOR);
										
					if (layer3D != null) 
					{
						//获取图层的地理范围,并飞行到该范围
						var geobound = layer3D.get_bounds();   
						scene.get_flyingOperator().flyToBounds(geobound);
					}
				}
				//为场景控件添加一个选择侦听事件。
				sceneControl.addEvent("objectSelected",getFieldandValue);
			}

			//控件初始化失败后的回调函数
			function failedCallback() 
			{
				alert("Realspace initialized failed!");
			}
			
			function pan()
			{
				//清除高亮
				var layer3D = scene.get_layer3Ds().get_item("buildings@RealspaceSample");
				layer3D.releaseSelection();
				//切换漫游Action
				var panAction = new SuperMap.Web.UI.Action3Ds.Pan(sceneControl);
				sceneControl.set_sceneAction(panAction); 	
			}
			
			function select_onclick()
			{
				var selectAction = new SuperMap.Web.UI.Action3Ds.Select(sceneControl);
				sceneControl.set_sceneAction(selectAction);	 
			}
	
			//当在场景控件上进行选择操作时,触发该事件。
			function getFieldandValue(selection3Ds)
			{
				var layer3D = scene.get_layer3Ds().get_item("buildings@RealspaceSample");
				var fieldInfo = "";
				fieldCount = layer3D.get_fieldInfos().get_count();
				for(var i=0;i<fieldCount;i++)
				{
					fieldInfo += layer3D.get_fieldInfos().get_item(i).get_name() + ":";
					fieldInfo += layer3D.getFieldValue(i) + "; ";
				}
				alert(fieldInfo);
			}
  
		</script>
	</head>


	<body onLoad="onPageLoad()">  	
	    <div style="position:absolute;top:5px;height:6%;width:100%;float: left">
		<input id="" type="button" value="查询" onclick="select_onclick()">
		<input id="Pan" type="button" value="漫游" onclick="pan()">
		</div>
		<div id="sceneControlDiv" style="position:absolute;bottom:5px;top:30px;height:100%;width:100%;float=left">
		</div>	
	</body>
</html>