Search code examples
c#xamlmouseeventsilverlight-5.0esri

Silverlight 5 c# how to capture the layer on i was clicked


i have the next problem, i'm making one app in silverlight 5 c# whit sdk Esri and i have de the XAML

<Grid x:Name="LayoutRoot" >
     <Grid.ColumnDefinitions>
            <ColumnDefinition Width="196*"/>
            <ColumnDefinition Width="7*"/>
            <ColumnDefinition Width="197*"/>
      </Grid.ColumnDefinitions>

     <esri:Map WrapAround="True" x:Name="MyMap"  Extent="-1082593,4487171,742111,5174493" ZoomFactor="4.0" Grid.ColumnSpan="3"/>

</Grid>

and the nextc c# code

 public MainPage()
    {
        InitializeComponent();

        var myLayer = new ArcGISDynamicMapServiceLayer();
        myLayer.Url = "http://192.168.1.165:6080/arcgis/rest/services/Myservice/";
        myLayer.ID = "servicioWeb";
        myLayer.Initialize();
        MyMap.Layers.Add(myLayer);

        MyMap.MouseClick += MyMap_MouseClick;        
    }

    void MyMap_MouseClick(object sender, Map.MouseEventArgs e)
    {
       I NEED HELP
    }

what code i have to put for catch the clic on ArcGISDynamicMapServiceLayer´s layer? I saw a lot of examples but only i saw examples with featureLayer and graphics.

Thanks for all.

Regards.


Solution

  • By the way you can't catch a click on ArcGISDynamicMapServiceLayer exactly, because ArcGISDynamicMapServiceLayer is just a simple image from server. If you click on Map, you always click on top layer in LayerCollection except Feature or Graphics Layers.

    FeatureLayer or GraphicsLayer are different, because it's "object" layers that contains some "real" graphics, not image.

    If you need to find some objects in ArcGISDynamicMapServiceLayer you should get MapCoords and make Identify or Geometry Queries to the rest service with your url, such as:

    MyMap.MouseClick += (s, e) =>
    {
        var queryTask = new QueryTask(URL);
        var query = new Query
        {
            Geometry = e.Point,
            OutSpatialReference = MyMap.SpatialReference,
            ReturnGeometry = true
        };
        query.OutFields.Add("*");
        queryTask.ExecuteCompleted += .. Some Completed Handler ..
        queryTask.Failed += .. Some Failed Handler ..
        queryTask.ExecuteAsync(query);
    }
    

    Note that using e.Point not very effective in identify. Instead of this you can make some simple buffer:

    const int resShift = 2;
    var env = new Envelope(e.Point.X - MyMap.Resolution * resShift, 
       e.Point.Y - MyMap.Resolution * resShift,
       e.Point.X + MyMap.Resolution * resShift, 
       e.Point.Y + MyMap.Resolution * resShift)
          {
             SpatialReference = MyMap.SpatialReference
          };