using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using ThinkGeo.MapSuite.SilverlightEdition; using System.Collections.ObjectModel; using System.Windows.Threading; using ThinkGeo.MapSuite.SilverlightCore; using System.Collections.Generic; namespace CSharp_Samples { public partial class RefreshPointsRandomly : UserControl { private readonly RectangleShape worldExtent = new RectangleShape(-180, 90, 180, -90); private DispatcherTimer redrawTimer; private DispatcherTimer changeColorTimer; public RefreshPointsRandomly() { InitializeComponent(); redrawTimer = new DispatcherTimer(); changeColorTimer = new DispatcherTimer(); } private void Map1_Loaded(object sender, RoutedEventArgs e) { Map1.MapUnit = GeographyUnit.DecimalDegree; Map1.CurrentExtent = new RectangleShape(-90, 90, 90, -90); WorldMapKitWmsSilverlightOverlay baseOverlay = new WorldMapKitWmsSilverlightOverlay(); Map1.Overlays.Add(baseOverlay); InMemoryFeatureLayer pointsLayer = new InMemoryFeatureLayer(); pointsLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Circle, new GeoSolidBrush(GeoColor.GetRandomGeoColor(RandomColorType.All)), 10); pointsLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Capital1("label"); pointsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; pointsLayer.Open(); pointsLayer.Columns.Add(new FeatureSourceColumn("label")); LayerOverlay pointsOverlay = new LayerOverlay(); pointsOverlay.Layers.Add("PointsLayer", pointsLayer); Map1.Overlays.Add("PointsOverlay", pointsOverlay); Map1.Refresh(); } private void BeginMovingPoints(int interval) { redrawTimer.Tick += new EventHandler(delegate { MovePoints(); }); redrawTimer.Interval = TimeSpan.FromMilliseconds(interval); redrawTimer.Start(); } private void BeginChangingColor(int interval) { changeColorTimer.Tick += new EventHandler(delegate { ChangeColor(); }); changeColorTimer.Interval = TimeSpan.FromMilliseconds(interval); changeColorTimer.Start(); } private void MovePoints() { LayerOverlay pointsOverlay = (LayerOverlay)Map1.Overlays["PointsOverlay"]; InMemoryFeatureLayer pointsLayer = (InMemoryFeatureLayer)pointsOverlay.Layers["PointsLayer"]; Collection features = new Collection(); for (int i = 0; i < pointsLayer.InternalFeatures.Count; i++) { PointShape shape = pointsLayer.InternalFeatures[i].GetShape() as PointShape; string id = pointsLayer.InternalFeatures[i].Id; PanDirection direction = (PanDirection)PanDirection.Parse(typeof(PanDirection), id, true); if (shape.X > worldExtent.UpperLeftPoint.X && shape.X < worldExtent.LowerRightPoint.X && shape.Y < worldExtent.UpperLeftPoint.Y && shape.Y > worldExtent.LowerRightPoint.Y) { UpdatePointShape(shape, direction); } else { id = ReverseDirection(shape, direction).ToString(); } Feature feature = new Feature(shape.X, shape.Y, id); feature.ColumnValues.Add("label", id); features.Add(feature); } pointsLayer.InternalFeatures.Clear(); foreach (var item in features) { pointsLayer.InternalFeatures.Add(item); } pointsOverlay.Refresh(); } private void ChangeColor() { LayerOverlay pointsOverlay = (LayerOverlay)Map1.Overlays["PointsOverlay"]; InMemoryFeatureLayer pointsLayer = (InMemoryFeatureLayer)pointsOverlay.Layers["PointsLayer"]; pointsLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Circle, new GeoSolidBrush(GeoColor.GetRandomGeoColor(RandomColorType.All)), 10); } private static PanDirection ReverseDirection(PointShape shape, PanDirection direction) { PanDirection newDirection = PanDirection.Down; switch (direction) { case PanDirection.Up: newDirection = PanDirection.Down; break; case PanDirection.UpperRight: newDirection = PanDirection.LowerLeft; break; case PanDirection.Right: newDirection = PanDirection.Left; break; case PanDirection.LowerRight: newDirection = PanDirection.UpperLeft; break; case PanDirection.Down: newDirection = PanDirection.Up; break; case PanDirection.LowerLeft: newDirection = PanDirection.UpperRight; break; case PanDirection.Left: newDirection = PanDirection.Right; break; case PanDirection.UpperLeft: newDirection = PanDirection.LowerRight; break; default: break; } UpdatePointShape(shape, newDirection); return newDirection; } private static void UpdatePointShape(PointShape shape, PanDirection direction) { switch (direction) { case PanDirection.Up: shape.Y++; break; case PanDirection.UpperRight: shape.X++; shape.Y++; break; case PanDirection.Right: shape.X++; break; case PanDirection.LowerRight: shape.X++; shape.Y--; break; case PanDirection.Down: shape.Y--; break; case PanDirection.LowerLeft: shape.X--; shape.Y--; break; case PanDirection.Left: shape.X--; break; case PanDirection.UpperLeft: shape.X--; shape.Y++; break; default: break; } } private void btnBegin_Click(object sender, RoutedEventArgs e) { LayerOverlay pointsOverlay = (LayerOverlay)Map1.Overlays["PointsOverlay"]; InMemoryFeatureLayer pointsLayer = (InMemoryFeatureLayer)pointsOverlay.Layers["PointsLayer"]; int pointsCount = Convert.ToInt32(((ComboBoxItem)cbxPointCount.SelectedItem).Content.ToString().Trim()); int redrawInterval = Convert.ToInt32(((ComboBoxItem)cbxRedrawInterval.SelectedItem).Content.ToString().Trim()); int colorChangeInterval = Convert.ToInt32(((ComboBoxItem)cbxChangeColorInterval.SelectedItem).Content.ToString().Trim()); pointsLayer.InternalFeatures.Clear(); for (int i = 0; i < pointsCount; i++) { PointShape pointShape = Helper.GetRandomPoint(worldExtent); string id = Helper.GetRandomDirection().ToString(); Feature feature = new Feature(pointShape.X, pointShape.Y, id); feature.ColumnValues.Add("label", id); pointsLayer.InternalFeatures.Add(feature); } pointsOverlay.Refresh(); BeginMovingPoints(redrawInterval); BeginChangingColor(colorChangeInterval); btnBegin.IsEnabled = false; btnStop.IsEnabled = true; } private void btnStop_Click(object sender, RoutedEventArgs e) { redrawTimer.Stop(); changeColorTimer.Stop(); btnBegin.IsEnabled = true; btnStop.IsEnabled = false; } } }