Select to view content in your preferred language

Create Graphics Layer

1167
5
11-30-2011 12:53 PM
JayKappy
Frequent Contributor
I am sorry for the stupid question...I am trying to get back to this and cant even remember how to create a nd populate a graphics layer.
I cant remember what to do in VB....do I need a Query?  Or can I simply assign the Grpahics layer to a MapService and index?

I trry the below adn I am getting a failed query...I knwo this is so easy...I am drawign a complete blank

XAML:
     <esri:GraphicsLayer ID="GraphicsLayer_TrafficCameras" MouseEnter="ReturnCamera"/>

VB:
    Private Sub ClickMe(ByVal sender As System.Object, ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
        Dim queryTrafficCameras As New QueryTask("http://gis.org/arcgis/rest/services/Test/MapServer/20")
        AddHandler queryTrafficCameras.ExecuteCompleted, AddressOf QueryTaskTrafficCameras_Execute
        AddHandler queryTrafficCameras.Failed, AddressOf QueryTaskTrafficCameras_Failed
        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.ReturnGeometry = True
        queryTrafficCameras.ExecuteAsync(query)
    End Sub

    Private Sub QueryTaskTrafficCameras_Execute(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
        ' Set the Target Graphics Layer
        Dim l_Cameras As GraphicsLayer = TryCast(Me.MyMap.Layers("GraphicsLayer_TrafficCameras"), GraphicsLayer)
        ' Clear previous results
        l_Cameras.ClearGraphics()

        ' Check for new results 
        Dim featureSet As FeatureSet = args.FeatureSet

        ' Get a feature count retunred from the Query and shwo that in a messagebox
        Dim Test As String = featureSet.Features.Count

        If featureSet.Features.Count > 0 Then
            ' Add results to map
            For Each resultFeature As Graphic In featureSet.Features
                resultFeature.Symbol = TryCast(LayoutRoot.Resources("StrobeMarkerSymbol_Red"), ESRI.ArcGIS.Client.Symbols.Symbol)
                l_Cameras.Graphics.Add(resultFeature)
            Next
        Else
            MessageBox.Show("No features found")
        End If
    End Sub

    Private Sub QueryTaskTrafficCameras_Failed(ByVal sender As Object, ByVal args As TaskFailedEventArgs)
        MessageBox.Show("Query failed: " & Convert.ToString(args.[Error]))
    End Sub
0 Kudos
5 Replies
DominiqueBroux
Esri Frequent Contributor
Not sure what is your error.
Did you check that your query returns some features?

Also, as you use a marker symbol, the geometry of the features must be 'Point'. Did you check that?

Sidenote : instead of using a graphicslayer and manage the query by yourself, you may also use a featurelayer, you wouldn't have to worry about the query (nor about the symbol from 10.0).
0 Kudos
JayKappy
Frequent Contributor
Thank for your thoughts...

RIght now I am tryign to simply fire the code by a MouseClick in the map.  I get the Message HELLO seen below...
BUT I NEVER get to the Private Sub QueryTaskTrafficCameras_Execute ... the messagebox with HELLO THERE never fires...

Why am I not getting there?

Error: Is now firing into the "QueryTaskTrafficCameras_Failed" giving this error
Query failed: ESRI.ArcGIS.Client.Tasks.ServiceException

Thanks

    Private Sub ClickMe(ByVal sender As System.Object, ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
        MessageBox.Show("HELLO")

        '' Run Query to Draw Traffic Camera Graphics Layer
        Dim queryTrafficCameras As New QueryTask("http://gis.org/arcgis/rest/services/Test/MapServer/20")
        AddHandler queryTrafficCameras.ExecuteCompleted, AddressOf QueryTaskTrafficCameras_Execute
        AddHandler queryTrafficCameras.Failed, AddressOf QueryTaskTrafficCameras_Failed
        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        'query.Geometry = e.MapPoint
        query.ReturnGeometry = True
        queryTrafficCameras.ExecuteAsync(query)

    End Sub


    Private Sub QueryTaskTrafficCameras_Execute(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
       
         MessageBox.Show("HELLO THERE")

        ' Set the Target Graphics Layer
        Dim l_Cameras As GraphicsLayer = TryCast(Me.MyMap.Layers("GraphicsLayer_TrafficCameras"), GraphicsLayer)
        ' Clear previous results
        l_Cameras.ClearGraphics()

        ' Check for new results 
        Dim featureSet As FeatureSet = args.FeatureSet

        ' Get a feature count retunred from the Query and shwo that in a messagebox
        Dim Test As String = featureSet.Features.Count
        MessageBox.Show("Count" & Test)

        If featureSet.Features.Count > 0 Then
            ' Add results to map
            For Each resultFeature As Graphic In featureSet.Features
                resultFeature.Symbol = TryCast(LayoutRoot.Resources("StrobeMarkerSymbol_Red"), ESRI.ArcGIS.Client.Symbols.Symbol)
                l_Cameras.Graphics.Add(resultFeature)
            Next
        Else
            MessageBox.Show("No features found")
        End If
    End Sub



I can get it to work like this...but want to Graphics layer for many reasons....

<esri:FeatureLayer ID="MG_Cameras" FeatureSymbol="{StaticResource StrobeMarkerSymbol_Red}" Visible="True" 
                                Url="http://gis.org/arcgis/rest/services/Test/MapServer/20" />
0 Kudos
JayKappy
Frequent Contributor
Very confused...if I do it as a feature layer is draws fine....so I know I can get to the data, know that its a point, know that the FeatureSymbol works fine...

Why am I erroring out?  The query seems pretty straight forward...syntax?  I figure if I was missing a reference or an Import it would tell me...I dont even get into the "AddHandler queryTrafficCameras.ExecuteCompleted, AddressOf QueryTaskTrafficCameras_Execute" ?

Anyone have any ideas?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I figure if I was missing a reference or an Import it would tell me...I dont even get into the "AddHandler queryTrafficCameras.ExecuteCompleted, AddressOf QueryTaskTrafficCameras_Execute" ?


You are not going into the 'ExecuteCompleted' handler since your query failed (i.e you are going to your QueryTaskTrafficCameras_Failed handler). That's normal.

Error: Is now firing into the "QueryTaskTrafficCameras_Failed" giving this error
Query failed: ESRI.ArcGIS.Client.Tasks.ServiceException


So now the question is why does your query fail?

My feeling is that you need to add at least one search criteria else the rest end point doesn't accept the query.
You can try by adding a dummy where clause : query.Where="1=1";

If still not working, use fiddler to look at the requests sent to the server, that may give a clue.
0 Kudos
JayKappy
Frequent Contributor
THanks....tried that and still nothing so looked on and it turns out I needed the where clause and an output spatial reference.

        query.Where = "1=1"
        query.OutSpatialReference = MyMap.SpatialReference

Everything works now....thanks....actually took a day or two to get back into this....have been away from it for a some time....

THANKS for your thoughts and help...very appreciated.
0 Kudos