Arcgis silverlight-5 layers总结

layers总结

   

    arcserver silverlight中,一个map由不同地图服务叠加组成,第一次加载的服务地图在最下方,最后加载的服务地图在最上方。每个地图服务又包含不同的地图图层。

    arcserver silverlight layer主要为分两种

    1)layer,指一个地图服务,相当于一个mxd文件,有ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,ArcGISImageServiceLayer三种类型,分别代表cached map service(已经生成地图缓存的map service)、non-cached map service(动态图层)和map imagery layer(影像图层)。

    2)Sublayer,指代广义上的图层(lyr),相当于每个服务(mxd文件)中的一个图层

 

以下分别进行介绍,设定当前网页map名称为mymap:

 

1、layer操作

1)控制不同layer的显示和透明度(例子参见Arcgis silverlight-3

方法:一般情况下用控件绑定mymap的layers,控制layer显示就拿控件绑定layer的visible属性,控制透明度就绑定layer的opacity

例如以下例子,用ListBox控件绑定mymap的layers属性,里面包含一个CheckBox和Slider,分别绑定layer的visibleopacity属性

 eg:

 

代码
   < ListBox x:Name = " MyList "  ItemsSource = " {Binding ElementName=MyMap, Path=Layers} " >
                
< ListBox.ItemTemplate >
                    
< DataTemplate >
                        
< StackPanel Orientation = " Horizontal " >
                            
<!-- Layer visibility checkbox -->
                            
< CheckBox IsChecked = " {Binding Visible, Mode=TwoWay} "   />
                            
<!-- Opacity slider -->
                            
< Slider Margin = " -5,0,0,0 "  Minimum = " 0 "  Maximum = " 1 "  Width = " 30 "  
                                Value
= " {Binding Opacity, Mode=TwoWay} "  Height = " 18 "   />
                            
<!-- Layer name -->
                            
< TextBlock Text = " {Binding ID, Mode=OneWay} "  Margin = " 5,0,0,0 "   >  
                            
<!--  Tooltip on hover -->
                                
< ToolTipService.ToolTip >
                                    
< StackPanel MaxWidth = " 400 " >
                                        
< TextBlock FontWeight = " Bold "  Text = " {Binding CopyrightText} "  TextWrapping = " Wrap "   />
                                        
< TextBlock Text = " {Binding Description} "  TextWrapping = " Wrap "   />
                                    
</ StackPanel >
                                
</ ToolTipService.ToolTip >
                            
</ TextBlock >
                        
</ StackPanel >
                    
</ DataTemplate >
                
</ ListBox.ItemTemplate >
            
</ ListBox >

 

2)不同layer之间的切换,每次只显示一个layer(一个地图服务)(例子参见http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SwitchMap

方法:一般情况下用选择型控件(RadioButton),当这个控件触发时,调用一个函数,切换当前地图服务的url

eg:

 

代码
private   void  RadioButton_Click( object  sender, RoutedEventArgs e)
        {
            
//  Get the Tag from the selected radio button.  The Tag value
            
//  cooresponds to the index value of a layer in the map.  
             int  index  =  Convert.ToInt32(((RadioButton)sender).Tag);

            ArcGISTiledMapServiceLayer arcgisLayer 
=  MyMap.Layers[ " AGOLayer " as  ArcGISTiledMapServiceLayer; // 视情况决定layer类型,TiledMapServiceLayer or dynamicLayer等

            
switch  (index)
            {
                
case   0 :
                    arcgisLayer.Url 
=   " http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer " ;
                    
break ;
                
case   1 :
                    arcgisLayer.Url 
=   " http://server.arcgisonline.com/ArcGIS/rest/services/NGS_Topo_US_2D/MapServer " ;
                    
break ;
                
case   2 :
                    arcgisLayer.Url 
=   " http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer " ;
                    
break ;
            }
        }

 

 

3)动态加载layer(地图服务)(例子参见http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AddLayerDynamically

方法:在MyMap.Layers中添加地图服务的url

eg:

 

代码
private   void  AddLayerButton_Click( object  sender, RoutedEventArgs e)
        {
            
// MyMap.Layers.Clear();  // 首先清空MyMap的所有Layers,如果希望地图只包含新添加的图层,可以添加这一句

            ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer NewTiledLayer 
=   new  ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer(); // 添加的新图层类型,即是哪种地图服务

            MyMap.Layers.LayersInitialized 
+=  (evtsender, args)  => // 以下4行将地图显示范围调整到新添加图层的范围,这个必须后添加的图层空间参考和当前map的空间参考相同,可删掉
            {
                MyMap.ZoomTo(NewTiledLayer.InitialExtent);             

            };

            NewTiledLayer.Url 
=  UrlTextBox.Text; // 新添加的图层服务url
            MyMap.Layers.Add(NewTiledLayer); // 将图层添加到mymap的layers中
        }

 

2、Sublayer操作

1)控制不同Sublayer的显示(例子参见Arcgis silverlight-4 Sublayerlist

 

代码
// 控制地图图层显示,获取CheckBox对应的图层ID,判断是否显示,存储到visibleLayerList,并将结果返回到dynamicServiceLayer.VisibleLayers,即可控制图层显示/不显示
         private   void  CheckBox_Click( object  sender, RoutedEventArgs e) 
        {
            CheckBox tickedCheckBox 
=  sender  as  CheckBox;

            
string  serviceName  =  tickedCheckBox.Name;
            
bool  visible  =  ( bool )tickedCheckBox.IsChecked;

            
int  layerIndex  =  ( int )tickedCheckBox.Tag;

            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer 
=  MyMap.Layers[serviceName]  as
                ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;

            List
< int >  visibleLayerList  =
                dynamicServiceLayer.VisibleLayers 
!=   null
                
?  dynamicServiceLayer.VisibleLayers.ToList() :  new  List < int > ();

            
if  (visible)
            {
                
if  ( ! visibleLayerList.Contains(layerIndex))
                    visibleLayerList.Add(layerIndex);
            }
            
else
            {
                
if  (visibleLayerList.Contains(layerIndex))
                    visibleLayerList.Remove(layerIndex);
            }

            dynamicServiceLayer.VisibleLayers 
=  visibleLayerList.ToArray();
        }
        
// 获取ArcGISDynamicMapServiceLayer地图服务初始化时,设定地图的dynamicServiceLayer.VisibleLayers 

        
private   void  ArcGISDynamicMapServiceLayer_Initialized( object  sender, EventArgs e)
        {
            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer 
=
                sender 
as  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
            
if  (dynamicServiceLayer.VisibleLayers  ==   null )
               dynamicServiceLayer.VisibleLayers 
=  GetDefaultVisibleLayers(dynamicServiceLayer);
        }
        
// 获取ArcGISDynamicMapServiceLayer服务提供的地图中的可视图层,返回一个可视图层的ID队列
         private   int [] GetDefaultVisibleLayers(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicService)
        {
            List
< int >  visibleLayerIDList  =   new  List < int > ();
            ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray 
=  dynamicService.Layers;
            
for  ( int  index  =   0 ; index  <  layerInfoArray.Length; index ++ )
            {
                
if  (layerInfoArray[index].DefaultVisibility)
                    visibleLayerIDList.Add(index);
            }
            
return  visibleLayerIDList.ToArray();
        }   

 

 3、element layer

    element layer用来显示一些地图元素,例如在地图上一个点用一个五角星来表示。 element layer也可以调用meida文件,具体在下一节中详细描述。

eg:

 

代码
< esri:Map x:Name = " MyMap " >
    
< esri:ArcGISTiledMapServiceLayer ID = " StreetMapLayer "  
                    Url
= " http://services.arcgisonline.com/ArcGIS/rest/services/NPS_Physical_World_2D/MapServer " />

    
< esri:ElementLayer >     //定义一个element layer
        
< esri:ElementLayer.Children >
            
<!-- Clickable button -->    //定义一个按钮
            
< Button x:Name = " RedlandsButton "  Width = " 20 "  Height = " 20 "  Content = " X "  
                        esri:ElementLayer.Envelope
= " -117,34,-117,34 "
                        VerticalAlignment
= " Center "  HorizontalAlignment = " Center "
                        Click
= " RedlandsButton_Click "   /> //按钮触发一个操作

            
<!-- Arrow pointing at Copenhagen from the right -->  //定义一个箭头
             < TextBlock Text = " &lt;= "  HorizontalAlignment = " Right "  
                           FontSize
= " 15 "  Foreground = " Blue "  FontWeight = " Bold "
                           esri:ElementLayer.Envelope
= " 12.5698,55.6765,12.5698,55.6765 "   />
            
<!-- Arrow pointing at Copenhagen from the left -->  //定义一个箭头
            
< TextBlock Text = " =&gt; "  HorizontalAlignment = " Left "  
                           FontSize
= " 15 "  Foreground = " Blue "  FontWeight = " Bold "
                           esri:ElementLayer.Envelope
= " 12.5698,55.6765,12.5698,55.6765 "   />

            
<!--  Red box  -  No size specified. Envelope guides the size  -->  //定义一个红色矩形
            
< Rectangle Fill = " Red "  esri:ElementLayer.Envelope = " 0,0,10,10 "   />

            
<!-- Editable textbox -->   //定义一个可编辑的textbox
             < TextBox Width = " 100 "  Height = " 20 "  esri:ElementLayer.Envelope = " 40,0,40,0 "
                         Text
= " Editable text "  HorizontalAlignment = " Right "  VerticalAlignment = " Bottom "   />
        
</ esri:ElementLayer.Children >
    
</ esri:ElementLayer >

</ esri:Map >

 

 

 

总结

    arcserver silverlight中,layer和Sublayer 处于上下级关系,layer操作调用Mymap.Layers,Sublayer操作调用mapservice.Layers

eg:        

   图层名称                   控制图层显示                   

   layer             Mymap.Layers visible属性

   Sublayer       mapservice.Layers visible图层id序列

 

  element layer用来显示一个地图元素,给定元素位置和元素类型,就可以显示这个元素,地图元素同样也可以触发操作。

   

你可能感兴趣的:(silverlight)