控制图层的现实
-------------------------------------------------------------------------------------------------------------
方法一:
-------------------------------------------------------------------------------------------------------------
/// <summary>
/// 控制图层是否显示
/// </summary>
/// <param name="strLayerName">图层名</param>
/// <param name="blChecked">是否显示图层</param>
/// <param name="Map1">Map控件</param>
public void LayerCheck(String strLayerName, bool blChecked, Map Map1)
{
IMapFunctionality mf = Map1.GetFunctionality("NorthAmerica") as IMapFunctionality;
IGISResource gisresource = mf.Resource;
IQueryFunctionality qfunc = null;
qfunc = (IQueryFunctionality)gisresource.CreateFunctionality(typeof(IQueryFunctionality), null);
string[] lids;
string[] lnames;
// 获取所有图层的id和name
qfunc.GetQueryableLayers(null, out lids, out lnames);
int layer_index = 0;
for (int i = 0; i < lnames.Length; i++)
{
// 找到符合要求的图层编号
if (lnames == strLayerName)
{
layer_index = i;
break;
}
}
if (strLayerName != string.Empty)
{
mf.SetLayerVisibility(layer_index.ToString(), blChecked);
}
Map1.Refresh();
}
-------------------------------------------------------------------------------------------------------------
方法二:
-----------------------------------------------------------------------------------------------
当需要控制图层的可见性时,可以使用
IMapDescription mapdescription = webMap.MapDescription;
webMap.ManageLifetime(mapdescription);
ILayerDescriptions layerdec = mapdescription.LayerDescriptions;
for(int i=0;i < mapdescription.LayerDescriptions.Count; i++)
{
ILayerDescription onelayerdesc = layerdec.get_Element(i);
onelayerdesc.Visible = true;
}
-----------------------------------------------------------------------------------------------
方法三:
-----------------------------------------------------------------------------------------------
读这篇文章的前提是看过CJ的“自定义Functionality”。
这篇文章是对一个想法的实现:如何定制显示图层。大家都知道map是由layers组成,同一地区的若干张地图实际上只有有限个图层组成,通过对这些图层的组合显示,我们就可以显示相关的地图。这样做的前提有两个:
1. 知道某一地图组成的图层列表;
2. 知道地图的范围;
这样,我们就可以从外部传入图层列表和地图的范围,显示我们希望看到的地图了。
通过以下代码,我们能了解到:
1. 如何从一个Map Service的所有图层中显示出我们想要的layers;(CJ的一篇文章中已提及,我只是改了改)
2. 如何在Web ADF中得到Session中的属性。用JSF中获得Session的办法行不通,只有求助于ExternalContext
package com.brsc;
import com.esri.adf.web.ags.data.AGSMapFunctionality;
………………
public class CustomLayerFunctionality implements GISFunctionality {
private ArrayList layerList;
private AGSMapResource resource;
//
public void destroyFunctionality() {
// TODO Auto-generated method stub
}
public GISResource getResource() {
// TODO Auto-generated method stub
return null;
}
public void initFunctionality(GISResource arg0) {
try {
//从session对象中得到 Layers 的 ArrayList
FacesContext cxt = WebUtil.getFacesContext();
if (cxt == null) {
return;
}
ExternalContext excxt = cxt.getExternalContext();
if (excxt == null) {
return;
}
java.util.Map layerSessionMap = excxt.getSessionMap();
layerList = (ArrayList) layerSessionMap.get("layerList");
//
this.resource=(AGSMapResource)arg0;
AGSMapFunctionality mapfunc=(AGSMapFunctionality)resource.getFunctionality("map");
MapServerInfo serverInfo=mapfunc.getMapServerInfo();
//图的旋转角度
//serverInfo.getDefaultMapDescription().setRotation(60);
//创建所需图层
serverInfo.getDefaultMapDescription().setLayerDescriptions(CreateNewlayerDescription(layerList,serverInfo));
//设定地图范围
HashMap hmap=(HashMap)layerSessionMap.get("mapEnvelope");
if(hmap==null){
return;
}
WebExtent ext=mapfunc.getFullExtent();
//
double minx=((Double)hmap.get("minx")).doubleValue();
double miny=((Double)hmap.get("miny")).doubleValue();
double maxx=((Double)hmap.get("maxx")).doubleValue();
double maxy=((Double)hmap.get("maxy")).doubleValue();
//判断是否在地图范围内
WebPoint pMin=new WebPoint(minx,miny);
WebPoint pMax=new WebPoint(maxx,maxy);
if(!ext.contains(pMin)){
return;
}
if(!ext.contains(pMax)){
return;
}
ext.setMinX(minx);
ext.setMinY(miny);
ext.setMaxX(maxx);
ext.setMaxY(maxy);
//设定初始范围
mapfunc.setFullExtent(ext);
mapfunc.setInitialExtent(ext);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 根据session中的图层列表创建新的LayerDescription
* @param LayerList 图层列表
* @param ServerInfo
* @return LayerDescription[]
*/
private LayerDescription[] CreateNewlayerDescription(ArrayList LayerList,MapServerInfo ServerInfo){
MapLayerInfo[] oldLayerInfos=ServerInfo.getMapLayerInfos();
MapLayerInfo[] mapLayerInfos=new MapLayerInfo[LayerList.size()];
for(int i=0;i<mapLayerInfos.length;i++){
mapLayerInfos=AGSUtil.getLayerInfo((String)LayerList.get(i), oldLayerInfos);
}
ServerInfo.setMapLayerInfos(mapLayerInfos);
//
LayerDescription[] oldLayerDes=ServerInfo.getDefaultMapDescription().getLayerDescriptions();
LayerDescription[] layerDescriptions=new LayerDescription[mapLayerInfos.length];
for(int j=0;j<layerDescriptions.length;j++){
layerDescriptions[j]=AGSUtil.getLayerDescription(mapLayerInfos[j].getLayerID(), oldLayerDes);
}
return layerDescriptions;
}
//
public ArrayList getLayerList() {
return layerList;
}
public void setLayerList(ArrayList layerList) {
this.layerList = layerList;
}
}
加油,哥们,现在开始!