using System; using System.Collections.Generic; 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 UseDraggableMarker : UserControl { private Marker draggingMarker; private double orignScreenX; private double orignScreenY; private IEnumerable associateMarkers; private const double searchRadius = 20; public UseDraggableMarker() { InitializeComponent(); } private void WpfMap_Loaded(object sender, RoutedEventArgs e) { wpfMap1.MapUnit = GeographyUnit.DecimalDegree; wpfMap1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9); WorldMapKitWmsWpfOverlay worldMapKitOverlay = new WorldMapKitWmsWpfOverlay(); wpfMap1.Overlays.Add(worldMapKitOverlay); SimpleMarkerOverlay markerOverlay = new SimpleMarkerOverlay(); markerOverlay.MarkerDragging += new EventHandler(markerOverlay_MarkerDragging); markerOverlay.MarkerDragged += new EventHandler(markerOverlay_MarkerDragged); markerOverlay.DragMode = MarkerDragMode.Drag; wpfMap1.Overlays.Add("MarkerOverlay", markerOverlay); Marker marker = new Marker(-95.2806, 38.9554); marker.MouseMove += new System.Windows.Input.MouseEventHandler(marker_MouseMove); marker.ImageSource = new BitmapImage(new Uri("/Resources/AQUA.png", UriKind.RelativeOrAbsolute)); marker.Width = 20; marker.Height = 34; marker.YOffset = -17; markerOverlay.Markers.Add(marker); Marker marker1 = new Marker(-96.2806, 38.9554); marker1.MouseMove += new System.Windows.Input.MouseEventHandler(marker_MouseMove); marker1.ImageSource = new BitmapImage(new Uri("/Resources/AQUA.png", UriKind.RelativeOrAbsolute)); marker1.Width = 20; marker1.Height = 34; marker1.YOffset = -17; markerOverlay.Markers.Add(marker1); wpfMap1.Refresh(); } void marker_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { if (draggingMarker != null) { if (sender == draggingMarker) { double newX = (double)draggingMarker.GetValue(Canvas.LeftProperty); double newY = (double)draggingMarker.GetValue(Canvas.TopProperty); double offsetX = newX - orignScreenX; double offsetY = newY - orignScreenY; foreach (Marker marker in associateMarkers) { if (marker != draggingMarker) { marker.SetValue(Canvas.LeftProperty, (double)marker.GetValue(Canvas.LeftProperty) + offsetX); marker.SetValue(Canvas.TopProperty, (double)marker.GetValue(Canvas.TopProperty) + offsetY); } } orignScreenX = newX; orignScreenY = newY; } } } void markerOverlay_MarkerDragged(object sender, MarkerDraggedSimpleMarkerOverlayEventArgs e) { draggingMarker = null; SimpleMarkerOverlay markerOverlay = (SimpleMarkerOverlay)sender; foreach (Marker marker in associateMarkers) { double newX = (double)marker.GetValue(Canvas.LeftProperty); double newY = (double)marker.GetValue(Canvas.TopProperty); PointShape worldCoordinate = markerOverlay.MapArguments.ToWorldCoordinate(new PointShape(newX, newY)); marker.Position = new Point(worldCoordinate.X, worldCoordinate.Y); marker.Refresh(); } } void markerOverlay_MarkerDragging(object sender, MarkerDraggingSimpleMarkerOverlayEventArgs e) { draggingMarker = e.DraggingMarker; orignScreenX = (double)draggingMarker.GetValue(Canvas.LeftProperty); orignScreenY = (double)draggingMarker.GetValue(Canvas.TopProperty); SimpleMarkerOverlay markerOverlay = (SimpleMarkerOverlay)sender; double unitLength = markerOverlay.MapArguments.CurrentResolution * searchRadius; RectangleShape searchArea = new RectangleShape(draggingMarker.Position.X - unitLength, draggingMarker.Position.Y + unitLength, draggingMarker.Position.X + unitLength, draggingMarker.Position.Y - unitLength); associateMarkers = markerOverlay.Markers.Where(m => { return searchArea.Contains(new PointShape(m.Position.X, m.Position.Y)); }); } } }