Select to view content in your preferred language

Add Feature Layer by Name?

3845
10
04-22-2011 09:37 AM
TonyThatcher
Deactivated User
I believe that I already know the answer to this.  But is it possible to add a FeatureLayer by Name, and not the Index Number? 

We are having to work off a single Map Service that will likely be adding and removing layers from the Service from time to time.  For some web applicaitons this works well because we want to display the map layers in entire Map Service.  Some web applications, we only want to display a sub-set of the service, so we are using FeatureLayers to add the layer to the Silverlight app.  The only way that I can see to do this is to use the full URL, along with the Index Number of the layer we want to add.  The problem comes if the Map Service is updated and the index numbers change.  Then what used to be, say, the city boundary, is now a layer of gas stations, or something like that. 

I'd prefer to access the layer by the Layer Name, and not an ID.  Has anyone come up with a way around this?  A function that uses the Layer Name to return the Index Number of the layer?  Then dynamically build the URL and add the layer?

Thanks!

-Tony
0 Kudos
10 Replies
TonyThatcher
Deactivated User
[HTML][/HTML]OK.  Here's what I came up with.  I'm sure there are other ways to go about it, but this seems to work out pretty well for me.

In the Code Behind:

Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'MessageBox.Show("MainPage_Loaded")
        ' Create an ArcGISDynamicServiceLayer. The Map object (a Map class) was previously defined in XAML.
        Dim myArcGISDynamicMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer

        myArcGISDynamicMapServiceLayer.Url = "http://www..../ArcGIS/rest/services/Atlas/Atlas/MapServer"
        myArcGISDynamicMapServiceLayer.ID = "MCAtlasService"
        myArcGISDynamicMapServiceLayer.Visible = False
        AddHandler myArcGISDynamicMapServiceLayer.Initialized, AddressOf myArcGISDynamicMapServiceLayer_Initialized
        MyMap.Layers.Add(myArcGISDynamicMapServiceLayer)

    End Sub

Private Sub myArcGISDynamicMapServiceLayer_Initialized(ByVal sender As Object, ByVal e As EventArgs)
        'Throw New NotImplementedException
        Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = MyMap.Layers("MCAtlasService")
        Dim myLayerInfo() As ESRI.ArcGIS.Client.LayerInfo = myArcGISDynamicMapServiceLayer.Layers

        Dim nameArray() As String = {"Townships", "Sections", "City Limits", "Counties"}
        Dim basePath As String = "http://www..../ArcGIS/rest/services/Atlas/Atlas/MapServer/"
        Dim layerPath As String = "NA"

        If myLayerInfo.Length > 0 Then
            For Each l In nameArray
                Dim i As Integer
                For i = 0 To myLayerInfo.Length - 1
                    'MessageBox.Show(myLayerInfo(i).Name + " | " + l)
                    If myLayerInfo(i).Name = l Then
                        layerPath = basePath + myLayerInfo(i).ID.ToString
                        Dim myFeatureLayer As New ESRI.ArcGIS.Client.FeatureLayer
                        myFeatureLayer.Url = layerPath
                        myFeatureLayer.ID = myLayerInfo(i).Name
                        If Not (getLegend(myLayerInfo(i).Name) Is Nothing) Then
                            myFeatureLayer.FeatureSymbol = getLegend(myLayerInfo(i).Name)
                        End If
                        If Not (getRenderer(myLayerInfo(i).Name) Is Nothing) Then
                            myFeatureLayer.Renderer = getRenderer(myLayerInfo(i).Name)
                        End If
                        'myFeatureLayer.MapTip.

                        MyMap.Layers.Add(myFeatureLayer)
                    End If

                Next
            Next
        End If

    End Sub

    Private Function getLegend(ByVal layername As String)
        Select Case layername
            Case "Sections"
                getLegend = SectionsSymbol
            Case "Townships"
                getLegend = TownshipsSymbol
            Case "City Limits"
                getLegend = CityLimitSymbol
            Case "Counties"
                getLegend = CountiesSymbol
            Case Else
                getLegend = Nothing
        End Select

    End Function

    Private Function getRenderer(ByVal layername As String)
        Select Case layername
            Case "Major Roads"
                getRenderer = RoadRenderer
            Case Else
                getRenderer = Nothing
        End Select

    End Function

0 Kudos