如何使图钉可在 bingmap wpf 中的地图上拖动

问题描述 投票:0回答:2

我正在研究 bingmap wpf。我在鼠标的单击事件上创建了图钉。现在我需要使其可拖动并根据图钉位置跟踪坐标。任何人都知道如何使图钉可拖动以及我们需要在哪个函数中编写代码以在发布时进行更新。

提前非常感谢您

c# wpf bing-maps bing-api
2个回答
6
投票
Vector _mouseToMarker;
private bool _dragPin;
public Pushpin SelectedPushpin { get; set; }

void pin_MouseDown(object sender, MouseButtonEventArgs e)
{
  e.Handled = true;
  SelectedPushpin = sender as Pushpin;
  _dragPin = true;
  _mouseToMarker = Point.Subtract(
    map.LocationToViewportPoint(SelectedPushpin.Location), 
    e.GetPosition(map));
}

private void map_MouseMove(object sender, MouseEventArgs e)
{
  if (e.LeftButton == MouseButtonState.Pressed)
  {
    if (_dragPin && SelectedPushpin != null)
    {
      SelectedPushpin.Location = map.ViewportPointToLocation(
        Point.Add(e.GetPosition(map), _mouseToMarker));
      e.Handled = true;
    }
  }
}

0
投票
Peter Wone 的解决方案有效,但需要进行一些小修复。当 MouseButtonState.Pressed = False 时,使 SelectedPushpin 无效或使 _drag = false。 完整解决方案:

Vector _mouseToMarker; private bool _dragPin; public Pushpin SelectedPushpin { get; set; } private void pin_MouseDown(object sender, MouseButtonEventArgs e) { e.Handled = true; SelectedPushpin = sender as Pushpin; _dragPin = true; _mouseToMarker = System.Windows.Point.Subtract(myMap.LocationToViewportPoint(SelectedPushpin.Location), e.GetPosition(myMap)); } private void map_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { if (_dragPin && SelectedPushpin != null) { SelectedPushpin.Location =myMap.ViewportPointToLocation( System.Windows.Point.Add(e.GetPosition(myMap), _mouseToMarker)); e.Handled = true; } } else { _dragPin = false; } }
解决方案的工作原理如下:1)您将一个事件附加到所有图钉的 MouseDown 事件,这样当您在图钉上方按下鼠标时,选定的图钉就会被分配给该图钉,然后在下一步中使用该图钉来移动它。
2)只要连续按下mouseleftbutton,就可以使用地图组件的MouseMove事件中识别的pushpin将其移动到下一个位置,然后使用dragpin标志使selectedpushpin无效,当鼠标按下离开pushpin时不再连续拖动先前的位置选定的图钉。

© www.soinside.com 2019 - 2024. All rights reserved.