diff --git a/MapControl/Avalonia/Map.Avalonia.cs b/MapControl/Avalonia/Map.Avalonia.cs index 4d0f1624..e47c2e79 100644 --- a/MapControl/Avalonia/Map.Avalonia.cs +++ b/MapControl/Avalonia/Map.Avalonia.cs @@ -70,33 +70,55 @@ namespace MapControl { var point = e.GetCurrentPoint(this); - if (point.Pointer.Type == PointerType.Mouse && HandleMousePressed(point) || - point.Pointer.Type == PointerType.Touch && HandleTouchPressed(point)) + if (pointer1 == null && point.Pointer.Type == PointerType.Mouse && point.Properties.IsLeftButtonPressed || + pointer2 == null && point.Pointer.Type == PointerType.Touch && ManipulationModes != ManipulationModes.None) { point.Pointer.Capture(this); - SetTransformCenter(point.Position); + + if (pointer1 == null) + { + pointer1 = point.Pointer; + position1 = point.Position; + } + else + { + pointer2 = point.Pointer; + position2 = point.Position; + } } base.OnPointerPressed(e); } + private void HandlePointerReleased(IPointer pointer, bool releaseCapture) + { + if (pointer == pointer1 || pointer == pointer2) + { + if (pointer == pointer1) + { + pointer1 = pointer2; + position1 = position2; + } + + pointer2 = null; + + if (releaseCapture) + { + pointer.Capture(null); + } + } + } + protected override void OnPointerReleased(PointerReleasedEventArgs e) { - if (HandlePointerReleased(e.Pointer)) - { - EndMoveMap(); - e.Pointer.Capture(null); - } + HandlePointerReleased(e.Pointer, true); base.OnPointerReleased(e); } protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) { - if (HandlePointerReleased(e.Pointer)) - { - EndMoveMap(); - } + HandlePointerReleased(e.Pointer, false); base.OnPointerCaptureLost(e); } @@ -113,66 +135,14 @@ namespace MapControl } else if (e.Pointer.Type == PointerType.Mouse || ManipulationModes.HasFlag(ManipulationModes.Translate)) { + TranslateMap(new Point(position.X - position1.X, position.Y - position1.Y)); position1 = position; - MoveMap(position); } } base.OnPointerMoved(e); } - private bool HandleMousePressed(PointerPoint point) - { - var handled = pointer1 == null && point.Properties.IsLeftButtonPressed; - - if (handled) - { - pointer1 = point.Pointer; - position1 = point.Position; - } - - return handled; - } - - private bool HandleTouchPressed(PointerPoint point) - { - var handled = pointer2 == null && ManipulationModes != ManipulationModes.None; - - if (handled) - { - if (pointer1 == null) - { - pointer1 = point.Pointer; - position1 = point.Position; - } - else - { - pointer2 = point.Pointer; - position2 = point.Position; - } - } - - return handled; - } - - private bool HandlePointerReleased(IPointer pointer) - { - var handled = pointer == pointer1 || pointer == pointer2; - - if (handled) - { - if (pointer == pointer1) - { - pointer1 = pointer2; - position1 = position2; - } - - pointer2 = null; - } - - return handled; - } - private void HandleManipulation(IPointer pointer, Point position) { var oldDistance = new Vector(position2.X - position1.X, position2.Y - position1.Y); diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index bcbd8052..5be145c5 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -277,39 +277,11 @@ namespace MapControl viewCenter = new Point(ActualWidth / 2d, ActualHeight / 2d); } - /// - /// Moves the map by the difference of the specified position in view coordinates and a temporary - /// transform origin point that has been set before by a call to SetTransformCenter. Map movement - /// must be terminated by a call to EndMoveMap. MoveMap provides higher accuracy than TranslateMap. - /// - public void MoveMap(Point position) - { - if (transformCenter != null) - { - viewCenter = position; - UpdateTransform(); - } - } - - /// - /// Terminates map movement by the MoveMap method. - /// - public void EndMoveMap() - { - if (transformCenter != null) - { - ResetTransformCenter(); - UpdateTransform(); - } - } - /// /// Changes the Center property according to the specified translation in view coordinates. /// public void TranslateMap(Point translation) { - EndMoveMap(); - if (translation.X != 0d || translation.Y != 0d) { var center = ViewToLocation(new Point(viewCenter.X - translation.X, viewCenter.Y - translation.Y)); @@ -328,7 +300,11 @@ namespace MapControl /// public void TransformMap(Point center, Point translation, double rotation, double scale) { - if (rotation != 0d || scale != 1d) + if (rotation == 0d && scale == 1d) + { + TranslateMap(translation); + } + else { SetTransformCenter(center); @@ -352,12 +328,6 @@ namespace MapControl UpdateTransform(true); } - else - { - // More accurate than SetTransformCenter. - // - TranslateMap(translation); - } } /// diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index dd572a0f..5d7d418b 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -234,6 +234,24 @@ namespace MapControl { var center = new Point(mapRect.X + mapRect.Width / 2d, mapRect.Y + mapRect.Height / 2d); var position = parentMap.ViewTransform.MapToView(center); + + if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical && + !parentMap.InsideViewBounds(position)) + { + var location = parentMap.MapProjection.MapToLocation(center); + + if (location != null) + { + var coercedPosition = parentMap.LocationToView( + new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude))); + + if (coercedPosition.HasValue) + { + position = coercedPosition.Value; + } + } + } + var width = mapRect.Width * parentMap.ViewTransform.Scale; var height = mapRect.Height * parentMap.ViewTransform.Scale; var x = position.X - width / 2d; diff --git a/MapControl/WPF/Map.WPF.cs b/MapControl/WPF/Map.WPF.cs index 3afd7bdc..bf071579 100644 --- a/MapControl/WPF/Map.WPF.cs +++ b/MapControl/WPF/Map.WPF.cs @@ -19,6 +19,7 @@ namespace MapControl public static readonly DependencyProperty ManipulationModeProperty = DependencyPropertyHelper.Register(nameof(ManipulationMode), ManipulationModes.Translate | ManipulationModes.Scale); + private Point? mousePosition; private double mouseWheelDelta; static Map() @@ -72,21 +73,26 @@ namespace MapControl { if (Keyboard.Modifiers == ModifierKeys.None && CaptureMouse()) { - SetTransformCenter(e.GetPosition(this)); + mousePosition = e.GetPosition(this); } } private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { - EndMoveMap(); - ReleaseMouseCapture(); + if (mousePosition.HasValue) + { + mousePosition = null; + ReleaseMouseCapture(); + } } private void OnMouseMove(object sender, MouseEventArgs e) { - if (e.LeftButton == MouseButtonState.Pressed) + if (mousePosition.HasValue) { - MoveMap(e.GetPosition(this)); + var p = e.GetPosition(this); + TranslateMap(new Point(p.X - mousePosition.Value.X, p.Y - mousePosition.Value.Y)); + mousePosition = p; } } diff --git a/MapControl/WinUI/Map.WinUI.cs b/MapControl/WinUI/Map.WinUI.cs index 6b38e64d..68a87e4c 100644 --- a/MapControl/WinUI/Map.WinUI.cs +++ b/MapControl/WinUI/Map.WinUI.cs @@ -24,7 +24,7 @@ namespace MapControl public static readonly DependencyProperty MouseWheelZoomDeltaProperty = DependencyPropertyHelper.Register(nameof(MouseWheelZoomDelta), 0.25); - private bool mouseMoveEnabled; + private Point? mousePosition; private double mouseWheelDelta; public Map() @@ -53,9 +53,11 @@ namespace MapControl private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) { - if (mouseMoveEnabled || e.Delta.Rotation == 0d && e.Delta.Scale == 1d) + if (mousePosition.HasValue) { - MoveMap(e.Position); + var p = e.Position; + TranslateMap(new Point(p.X - mousePosition.Value.X, p.Y - mousePosition.Value.Y)); + mousePosition = p; } else if (e.PointerDeviceType != PointerDeviceType.Mouse) { @@ -67,20 +69,23 @@ namespace MapControl { var point = e.GetCurrentPoint(this); - mouseMoveEnabled = e.Pointer.PointerDeviceType == PointerDeviceType.Mouse && - e.KeyModifiers == VirtualKeyModifiers.None && - point.Properties.IsLeftButtonPressed; - - if (mouseMoveEnabled || e.Pointer.PointerDeviceType != PointerDeviceType.Mouse) + if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse && + e.KeyModifiers == VirtualKeyModifiers.None && + point.Properties.IsLeftButtonPressed && + CapturePointer(e.Pointer)) { - SetTransformCenter(point.Position); + mousePosition = point.Position; } } private void OnPointerReleased(object sender, PointerRoutedEventArgs e) { - mouseMoveEnabled = false; - EndMoveMap(); + if (mousePosition.HasValue && + e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) + { + mousePosition = null; + ReleasePointerCapture(e.Pointer); + } } private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) diff --git a/SampleApps/WinUiApp/MainWindow.xaml b/SampleApps/WinUiApp/MainWindow.xaml index 0f2aae39..4a96c83b 100644 --- a/SampleApps/WinUiApp/MainWindow.xaml +++ b/SampleApps/WinUiApp/MainWindow.xaml @@ -147,6 +147,7 @@