Select to view content in your preferred language

Issue with graphicslayer

840
3
05-21-2012 04:38 AM
Labels (1)
AlexanderMagnusson
Deactivated User
Hello.

I'm messing around some and trying things out. One thing I'd like to be able to do is to display a number of symbols, and move them around while the application is running.

I've tried implementing this idea with the following code, it doesn't work with the obvious symptom being that the symbols doesn't move. If there's any questions regarding the code I'll gladly answer any questions.

 private void button2_Click(object sender, RoutedEventArgs e)
    {    
      graphicsLayer = new GraphicsLayer();
      symbolList_ = new List<ESRI.ArcGIS.Client.Geometry.MapPoint>();
      r = new Random();

      for (int i = 0; i < NUM_SYMBOLS; ++i)
      {
        symbolList_.Add(new ESRI.ArcGIS.Client.Geometry.MapPoint() 
        { SpatialReference = defaultSpatialReference,
           X = 0,
           Y = 0, 
        });

        graphicsLayer.Graphics.Add(new ESRI.ArcGIS.Client.Graphic()
        {
          Geometry = symbolList_,
          Symbol = s,
        });
      }
      
      _mapControl.Layers.Add(graphicsLayer);


      TimerCallback tcb = this.SymbolUpdater;
      System.Threading.Timer t = new System.Threading.Timer(tcb, null, 1000, Timeout.Infinite); 
    }

    public void SymbolUpdater(Object o)
    {
      while (true)
      {
        foreach (ESRI.ArcGIS.Client.Geometry.MapPoint mp in symbolList_)
        {
          mp.X += getRandom();
          mp.Y += getRandom();
        }
      }
    }

    private double getRandom()
    {
      double d = r.NextDouble();
      if (r.NextDouble() > 0.5)
      {
        return d;
      }
      else
        return -d;
    }
  }


Help is much appreciated. 🙂

Maybe this post could've gone into this thread: http://forums.arcgis.com/threads/48369-Graphics-Layer-inaccessible-by-background-thread
I apologize for any inconvenience if so.
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
You should be able to update Geometry to show movement in GraphicsLayer.

You can try the following code:
XAML-code
xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
 <Grid>
  <Grid.Resources>
   <esri:SimpleMarkerSymbol x:Key="MySymbol" Color="Red" Size="10" Style="Circle"/>
   <esri:SimpleRenderer x:Key="MyRenderer" Symbol="{StaticResource MySymbol}"/>
  </Grid.Resources>
  <esri:Map x:Name="MyMap">
   <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
   <esri:GraphicsLayer ID="MyGraphicsLayer" Renderer="{StaticResource MyRenderer}" />
  </esri:Map>
 </Grid>


C# Code-behind
  public MainWindow()
  {
   InitializeComponent();
   
   MyMap.Layers.LayersInitialized += (s, e) =>
   {
     AddGraphics();
     MoveGraphics();
   };
  }

  private void AddGraphics()
  {
   var layer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
   double xmin = MyMap.Layers[0].FullExtent.XMin;
   double ymin = MyMap.Layers[0].FullExtent.YMin;
   var random = new Random();
   for (int i = 0; i < 10; i++)
   {
    double x = xmin + (random.NextDouble() * 40000000);
    double y = ymin + (random.NextDouble() * 40000000);
    layer.Graphics.Add(new Graphic() { Geometry = new MapPoint(x, y) });
   }
  }

  private void MoveGraphics()
  {
   var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(30)};
   DateTime _lastOutput = DateTime.Now;
   var layer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
   int moveAmount = 180000;
   var random = new Random();
   timer.Tick += (s, e) =>
   {
    foreach (var g in layer.Graphics)
    {
     MapPoint pt = g.Geometry as MapPoint;
     pt.X += (random.NextDouble() - 0.5) * moveAmount;
     pt.Y += (random.NextDouble() - 0.5) * moveAmount;
    }
    var now = DateTime.Now;
    if (now - _lastOutput > TimeSpan.FromSeconds(1))
    {
     _lastOutput = now;
    }
   };
   timer.Start();
  }
0 Kudos
AlexanderMagnusson
Deactivated User
Hi Jenny, thanks for your reply, it really helped me.

There were mainly two issues with the code I posted. Firstly, the method of threading, and secondly the magnitude of movement. Adding a value between minus one and one didn't cause much of a noticeable movement. Multiplying by 50000 or so gives more visible results. Heh, such a simple thing to get stuck at.

By the way after a quick look in the documentation I haven't found any information regarding the used coordinate system. If anyone can direct me to it I'd be grateful.
0 Kudos
JenniferNery
Esri Regular Contributor
0 Kudos