Select to view content in your preferred language

How to add a wms layer to map dynamically?

4358
9
03-20-2011 07:56 PM
RenZhifeng
Deactivated User
I am trying to add/switch map services in my silverlight app. But when adding wms services, error occurs.

Codes:
            Map.Layers.Clear();
            WmsLayer layer = new WmsLayer()
            {
                Url = m_mapUrl[ServicesList.SelectedItems[0].ToString()],
                SkipGetCapabilities=false
            };
            layer.Initialized += (evtsender, args) =>
            {
                string[] myVisibleWmsLayers = new string[myWmsLayer.LayerList.Count];
                for (int i = 0; i < myWmsLayer.LayerList.Count; i++)
                {
                    myVisibleWmsLayers = i.ToString();
                }
                layer.Layers = myVisibleWmsLayers;
            };
            Map.Layers.Add(layer);

Please help me with this error or post any example. THANKS.
0 Kudos
9 Replies
DominiqueBroux
Esri Frequent Contributor
At first glance your code looks correct (on condition that your layers are called "1", "2", "3", ...).

What is the error you get?

Try to hook up an handler to the InitializatinFailed event, you might get some interesting info,
else try to use fiddler to look at the web requests.
0 Kudos
RenZhifeng
Deactivated User
I tried to hook up that handle...but failed. My codes follows:
-----------xaml------------
                    <esri:Map x:Name="Map" Background="White" Loaded="Map_Loaded">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseEnter">
                                <ei:ChangePropertyAction TargetName="myMenuItems" PropertyName="Visibility">
                                    <ei:ChangePropertyAction.Value>
                                        <Visibility>Collapsed</Visibility>
                                    </ei:ChangePropertyAction.Value>
                                </ei:ChangePropertyAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <esri:Map.Extent>
                            <esri:Envelope
                                XMin="-128.697684168741" YMin="31.9435680993751" XMax="-113.378846085436" YMax="44.0501181469211">
                                <esri:Envelope.SpatialReference>
                                    <esri:SpatialReference WKID="4269"/>
                                </esri:Envelope.SpatialReference>
                            </esri:Envelope>
                        </esri:Map.Extent>
                        <esri:ArcGISDynamicMapServiceLayer ID="DynamicLayer"
                Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapSer..."    Initialized="WmsLayer_Initialized"/>
                        <esri:GraphicsLayer ID="MySelectionGraphicsLayer" />
                    </esri:Map>
-------------c#--------------------
        private void WmsLayer_Initialized(object sender, System.EventArgs e)
        {
            WmsLayer layer = null;
            layer = new WmsLayer()
            {
                Url = m_mapUrl["http://serverapps.esri.com/ArcGIS/services/California/MapServer/WMSServer"],
                SkipGetCapabilities = false
            };
            layer.Initialized += (evtsender, args) =>
            {
                string[] myVisibleWmsLayers = new string[layer.LayerList.Count];
                for (int i = 0; i < layer.LayerList.Count; i++)
                {
                    myVisibleWmsLayers = i.ToString();
                }
                layer.Layers = myVisibleWmsLayers;
            };
            layer.InitializationFailed += (evtsender, args) =>
            {
                Layer layer1 = sender as Layer;
                if (layer1.InitializationFailure != null)
                {
                    MessageBox.Show(layer.ID + ":" + layer.InitializationFailure.ToString());
                }
            };
}

Is there any exmaple about this function?
THANKS
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The error you get is still unclear to me.

Nevertheless I tested your code and it's working well once I've added the layer to the map. The line 'Map.Layers.Add(layer);' is missing.

(and I replaced Url = m_mapUrl[http://serverapps.esri.com/ArcGIS/services/California/MapServer/WMSServer], by Url = "http://serverapps.esri.com/ArcGIS/services/California/MapServer/WMSServer" since m_mapUrl is internal to your project).
0 Kudos
JenniferNery
Esri Regular Contributor
The issue is mixed spatial reference. The service you used for your ArcGISDynamicMapServiceLayer is in SR=4326, the service you used for WMSLayer is in SR=3857.

If you keep your code and replace only the Map.Extent with the following, you will see the WMSLayer but your ArcGISDynamicMapServiceLayer will not be visible in this SpatialReference:
<esri:Map.Extent>
 <esri:Envelope XMin="-14930991.170" YMin="3611744.037" XMax="-11348896.882" YMax="5340571.181">
  <esri:Envelope.SpatialReference>
   <esri:SpatialReference WKID="3857"/>
  </esri:Envelope.SpatialReference>
 </esri:Envelope>
</esri:Map.Extent>


The following documentation might help: http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Map~Spatia...
0 Kudos
RenZhifeng
Deactivated User
yeah, the missing line is just my problem...sorry for that...thank you, BROUX.
0 Kudos
ibrahimKalayci
Emerging Contributor
Hi,

I would like to add one wmslayer dynamically in my silverlight application. i can add wms if I enter the layers as String Array, but before adding the list of layername must be shown in a listbox. here is my code. what is wrong. I get a blank screen if I run it.


  private ObservableCollection<wmsTitelName> _lwms = new ObservableCollection<wmsTitelName>();
  private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                listBox2.SelectionMode = SelectionMode.Multiple;
                _lwms.Clear();
                WmsLayer w = new WmsLayer();
                w.Url = textBox1.Text;
                w.ProxyUrl = "http://myPPage/ProxyPage/proxy.ashx";
                int[] iA = { 31467, 4326 };
                w.SupportedSpatialReferenceIDs = iA;
                w.Version = comboBox1.SelectedItem.ToString();
                w.SkipGetCapabilities = false;
                w.Initialize();
                w.Initialized += (sev, eve) =>
                {
                    for (int i = 0; i < w.LayerList.Count; i++)
                    {
                        wmsTitelName wmtn = new wmsTitelName()
                        {
                            Name = w.LayerList.Name,
                            Titel = w.LayerList.Title
                        };
                        _lwms.Add(wmtn);
                    }
                };

                listBox2.ItemsSource = _lwms;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
0 Kudos
JenniferNery
Esri Regular Contributor
You have not set WmsLayer.VisibleLayers property, this may be why you are seeing blank layer. If this property will be set based on ListBox.SelectedItems, you should handle that in your code and convert ObservableCollection<wmsTitelName> to string [] with visible layer names.
0 Kudos
ibrahimKalayci
Emerging Contributor
Hi,
I think that the problem is access. I've checked with fiddler. I get errors from proxy and  without ProxyURL ist clientaccesspolicy.xml missing. i can load the wmslayer but not layernames in listbox.  with this code i can load layernames from WMSsample in ArcGis samples but not other server.

case 2: //Add WMS to Map
                       try
                       {
                          
                           ESRI.ArcGIS.Client.Toolkit.DataSources.WmsLayer newLayer = new ESRI.ArcGIS.Client.Toolkit.DataSources.WmsLayer();
                           newLayer.Url = textBox1.Text;
                           newLayer.SkipGetCapabilities = true;
                          string[] na= new string[listBox2.SelectedItems.Count];
                       
                  for (int i = 0; i < listBox2.SelectedItems.Count; i++)
                  {
                      na = listBox2.SelectedItems.ToString();
                  }
               
                
                           newLayer.Layers= na;
                           newLayer.Version = textBox3.Text;
                           mainmap.Layers.Add(newLayer);
                       }
                       catch (Exception ex)
                       {
                           MessageBox.Show(ex.Message);
                       }
private void button2_Click(object sender, RoutedEventArgs e) //"Layers" button to get Layernames.
        {
            listBox2.SelectionMode = SelectionMode.Multiple;
            WmsLayer w = new WmsLayer();
            w.Url = textBox1.Text;
            w.Version = textBox3.Text;
            w.SkipGetCapabilities = false;
            w.Initialize();
            w.Initialized += (sev, eve) =>
            {
                for (int i = 0; i < w.LayerList.Count; i++)
                {
                    listBox2.Items.Add(w.LayerList.Name);
                   
                }
            };
0 Kudos
JenniferNery
Esri Regular Contributor
The following links might help:

Client-access policy error:
http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2009/08/24/Troubleshooting-blank-layers.aspx

Download proxy page:
http://help.arcgis.com/en/webapi/silverlight/help/index.html#/Secure_services/016600000022000000/

You can also try this small sample that was adapted from this SDK Sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#WmsLayerSimple

xmlns:esri="http://schemas.esri.com/arcgis/client/2009">

    <Grid x:Name="LayoutRoot" Background="White">
  <esri:Map x:Name="MyMap"  WrapAround="True" Extent="-15000000,2000000,-7000000,8000000">
   <esri:ArcGISTiledMapServiceLayer 
                Url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
   
  </esri:Map>
  <StackPanel VerticalAlignment="Top" HorizontalAlignment="Center">
   <Button Content="Add WMS" Click="Button_Click"/>   
   <ListBox x:Name="LayerList" SelectionMode="Multiple" SelectionChanged="LayerList_SelectionChanged">
    <ListBox.ItemTemplate>
     <DataTemplate>
      <TextBlock Text="{Binding Name}"/>
     </DataTemplate>
    </ListBox.ItemTemplate>
   </ListBox>
  </StackPanel>
 </Grid>


private void Button_Click(object sender, RoutedEventArgs e)
  {
   var wmsLayer = new WmsLayer()
   {
    ID="MyWmsLayer",
    Url = "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
    ProxyUrl = "http://serverapps.esri.com/SilverlightDemos/ProxyPage/proxy.ashx",
    SkipGetCapabilities = false,
    Version = "1.1.1",
    Opacity = 0.7,
    //Layers =new string[]{ "nexrad-n0r"}
   };

   wmsLayer.Initialized += (a, b) =>
    {
     LayerList.ItemsSource = (a as WmsLayer).LayerList;
    };
   MyMap.Layers.Add(wmsLayer);
  }

  private void LayerList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {

   var layerList = new List<string>();
   var wmsLayer = MyMap.Layers["MyWmsLayer"] as WmsLayer;
   if (wmsLayer.Layers != null)
   {
    foreach (var l in wmsLayer.Layers)
     layerList.Add(l);
   }
   if (e.AddedItems != null)
   {
    foreach(var item in e.AddedItems)
    {
     var info = item as WmsLayer.LayerInfo;
     layerList.Add(info.Name);
    }
   }
   if (e.RemovedItems != null)
   {
    foreach (var item in e.RemovedItems)
    {
     var info = item as WmsLayer.LayerInfo;
     layerList.Remove(info.Name);
    }
    
   }
   wmsLayer.Layers = layerList.ToArray();
  }
0 Kudos