using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.WpfDesktopEdition; namespace CSHowDoISamples { public partial class AddAnInMemoryMarkerOverlay : UserControl { private Collection drawingFeatures; private Marker draggingMarker; private Point lastPosition; private double resolution; public AddAnInMemoryMarkerOverlay() { InitializeComponent(); drawingFeatures = new Collection(); } private void WpfMap_Loaded(object sender, RoutedEventArgs e) { wpfMap1.MapUnit = GeographyUnit.DecimalDegree; wpfMap1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9); wpfMap1.MouseMove += new System.Windows.Input.MouseEventHandler(wpfMap1_MouseMove); wpfMap1.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(wpfMap1_MouseLeftButtonUp); WorldMapKitWmsWpfOverlay worldMapKitOverlay = new WorldMapKitWmsWpfOverlay(); wpfMap1.Overlays.Add(worldMapKitOverlay); InMemoryMarkerOverlay markerOverlay = new InMemoryMarkerOverlay(); markerOverlay.Drawn += new EventHandler(markerOverlay_Drawn); markerOverlay.Columns.Add(new FeatureSourceColumn("Name")); markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.ImageSource = new BitmapImage(new Uri("/Resources/AQUA.png", UriKind.RelativeOrAbsolute)); markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.Width = 20; markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.Height = 34; markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.YScreenOffset = -17; markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.ToolTip = "This is [#Name#]."; markerOverlay.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; wpfMap1.Overlays.Add("MarkerOverlay", markerOverlay); Feature newFeature = new Feature(-95.2806, 38.9554); newFeature.ColumnValues.Add("Name", "Lawrence"); markerOverlay.FeatureSource.BeginTransaction(); markerOverlay.FeatureSource.AddFeature(newFeature); markerOverlay.FeatureSource.CommitTransaction(); wpfMap1.Refresh(); } private void wpfMap1_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (draggingMarker != null) { PointShape newPoint = new PointShape(draggingMarker.Position.X, draggingMarker.Position.Y); Feature draggingFeature = (Feature)draggingMarker.Tag; Feature newFeature = new Feature(newPoint.GetWellKnownBinary(), draggingFeature.Id); foreach (KeyValuePair keyValue in newFeature.ColumnValues) { newFeature.ColumnValues.Add(keyValue.Key, keyValue.Value); } InMemoryMarkerOverlay markerOverlay = (InMemoryMarkerOverlay)wpfMap1.Overlays["MarkerOverlay"]; markerOverlay.FeatureSource.BeginTransaction(); markerOverlay.FeatureSource.UpdateFeature(newFeature); markerOverlay.FeatureSource.CommitTransaction(); draggingMarker = null; } } private void wpfMap1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { if (draggingMarker != null) { Point currentPosition = e.GetPosition(wpfMap1); double offsetX = (currentPosition.X - lastPosition.X) * resolution; double offsetY = (currentPosition.Y - lastPosition.Y) * resolution; draggingMarker.Position = new Point(draggingMarker.Position.X + offsetX, draggingMarker.Position.Y - offsetY); draggingMarker.Draw(wpfMap1.CurrentExtent, wpfMap1.ActualWidth, wpfMap1.ActualHeight); lastPosition = currentPosition; } } private void markerOverlay_Drawn(object sender, DrawnOverlayEventArgs e) { InMemoryMarkerOverlay markerOverlay = (InMemoryMarkerOverlay)wpfMap1.Overlays["MarkerOverlay"]; foreach (Marker marker in markerOverlay.OverlayCanvas.Children) { marker.MouseDown += new System.Windows.Input.MouseButtonEventHandler(marker_MouseDown); } } private void marker_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { draggingMarker = (Marker)sender; resolution = Math.Max(wpfMap1.CurrentExtent.Width / wpfMap1.ActualWidth, wpfMap1.CurrentExtent.Height / wpfMap1.ActualHeight); Point markerPosition = draggingMarker.Position; PointShape markerPositionShape = new PointShape(markerPosition.X, markerPosition.Y); InMemoryMarkerOverlay markerOverlay = (InMemoryMarkerOverlay)wpfMap1.Overlays["MarkerOverlay"]; drawingFeatures = markerOverlay.FeatureSource.GetFeaturesInsideBoundingBox(wpfMap1.CurrentExtent, ReturningColumnsType.NoColumns); Feature draggingFeature = (from f in drawingFeatures where f.GetWellKnownText().Equals(markerPositionShape.GetWellKnownText()) select f).First(); draggingMarker.Tag = draggingFeature; lastPosition = e.GetPosition(wpfMap1); e.Handled = true; } } }