about after using measure behavior, the graph can't stay in the map

7940
24
09-28-2010 12:05 PM
DanDong
New Contributor
Hi,

I want to ask a question about how to make the polygon stay in the map after using the measure function. I mean I use the measure function to measure the distance of each side of a polygon(measuremode="polygon"), but when I double click the last vertex, the polygon disappear?? I want to keep this polygon. What should I add in the xmal file and cs file? Thank you very much!
0 Kudos
24 Replies
JenniferNery
Esri Regular Contributor
You must be referring to MeasureAction. The GraphicsLayer that contains the polygon used for Measure is added to the map once Measure is started and removed from the map once Measure has completed.

If you need this polygon to stay in your map, you need to create your own GraphicsLayer and add a copy of this polygon to your GraphicsLayer. Maybe do something like the following:  Note, however, that this code will be executed everytime a new layer is added to your map. You might need to tweak this to handle when you subscribe/unsubscribe to Graphics.CollectionChanged event.
Xaml-code:
  <esri:Map x:Name="MyMap">
   <esri:ArcGISTiledMapServiceLayer ID="MyBaseLayer" 
                Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
   <esri:GraphicsLayer ID="MyGraphicsLayer"/>
  </esri:Map>


Code-behind:
this.MyMap.Layers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Layers_CollectionChanged);
  }

  void Layers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  {
   if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
   {
    foreach(var item in e.NewItems)
     if(item is GraphicsLayer)
      (item as GraphicsLayer).Graphics.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Graphics_CollectionChanged);
   }
  }

  void Graphics_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  {
   if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
   {
    GraphicsLayer graphicsLayer = this.MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    if (graphicsLayer != null)
    {
     foreach (var item in e.NewItems)
     {
      if (item is Graphic)
      {
       Graphic g = item as Graphic;
       graphicsLayer.Graphics.Add(new Graphic() { Geometry = g.Geometry, Symbol = g.Symbol });
      }
     }
    }
   }
  }
0 Kudos
DanDong
New Contributor
Thank you very much for the help. I was however trying to implement the code using c# and was errors due to my unfamiliarity with the language. Could you help me out by converting the code to c#?

Thanks so much.
0 Kudos
JenniferNery
Esri Regular Contributor
I'm assuming you already looked at this sample for MeasureAction:http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#UtilityActions

In addition to this sample, you can add the code I posted below. It is already in C#.
0 Kudos
DanDong
New Contributor
I'm assuming you already looked at this sample for MeasureAction:http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#UtilityActions

In addition to this sample, you can add the code I posted below. It is already in C#.


Hi, yes, I'v already seen the sample, but in the sample, when you double click the last vertex, the graph will disappear too.

I can't see the code you posted below, sorry, can you post it again? Thanks a lot!
0 Kudos
JenniferNery
Esri Regular Contributor
Kindly see post #2 for the additional code.
0 Kudos
DanDong
New Contributor
Kindly see post #2 for the additional code.


Thank you so much! It works!:)
0 Kudos
PatrickBrooke
New Contributor II
OK,

It may just be me, but this code snippet only captures the polyline(s), not the fill polygon. It appears that what happens is when the graphic is first added by measure action, it only adds the fill polygon on the initial click, at which time the geometry of the fill polygon is not known to the system so it cannot tell my new graphics layer what it's geometry is when I finish drawing.

Is there an easy way to get that fill polygon? I can think of a difficult way to do it maybe, but not an easy way. Help?
0 Kudos
JenniferNery
Esri Regular Contributor
You are right, in this sample, what gets added are Point and Polyline features only. The snippet here was meant to be a guideline on how you can retain the graphics in your map if the layer that contained them were added/removed by some means. I added a note in my previous post that the code also needs some tweaking 🙂

Adding the fill of the polygon and its additional area information to your GraphicsLayer is not something you can do with our API at this point. This sounds like an enhancement request that I need to forward to our team lead.
0 Kudos
JenniferNery
Esri Regular Contributor
Here's the answer I got after talking to our lead:

MeasureAction is designed to remove the graphic from the map after it is done. Should you need the graphic to stay in your map, you can use Draw object for that. Retaining the graphic drawn with MeasureAction is not supported.
0 Kudos