我正在寻找一种方法来显示在地图上拖动图钉的运动。我尝试修改此处提出的示例:http: //peteohanlon.wordpress.com/2010/10/10/draggable-pushpins/通过更新添加到 MouseMove 事件处理程序的委托中的 AssociatedObject.Location,但这没有结果. 在释放鼠标按钮之前,图钉一直保持在原位。然后它跳转到新位置。
知道如何强制 MapLayer 在拖动过程中跟踪图钉位置并在鼠标移动时正确重绘它?
我正在寻找一种方法来显示在地图上拖动图钉的运动。我尝试修改此处提出的示例:http: //peteohanlon.wordpress.com/2010/10/10/draggable-pushpins/通过更新添加到 MouseMove 事件处理程序的委托中的 AssociatedObject.Location,但这没有结果. 在释放鼠标按钮之前,图钉一直保持在原位。然后它跳转到新位置。
知道如何强制 MapLayer 在拖动过程中跟踪图钉位置并在鼠标移动时正确重绘它?
Max,你能澄清一下你想做什么吗?您在这里的方法听起来很合理,但是每次鼠标移动时让地图重新计算图钉的位置有点不需要。像这样的东西呢?:
当图钉进入拖动模式时,它会从地图中移除并替换为仅存在于屏幕空间中的可拖动图钉。所以用户在屏幕空间而不是地图空间中拖动“图钉”。
当用户结束拖动时,您将屏幕位置转换为地图位置(一个 Location 对象),然后将其添加回地图。
Silverlight Toolkit for WP7 中有一个通过 GestureListener 的现成解决方案:
<my:Pushpin Location="{Binding Location}">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted"/>
</toolkit:GestureService.GestureListener>
</my:Pushpin>
private void GestureListener_DragStarted(object sender, DragStartedGestureEventArgs e)
{
Map.IsEnabled = false;
}
private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
{
Map.IsEnabled = true;
}
private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
{
Point p = e.GetPosition(Map);
App.ViewModel.Location = Map.ViewportPointToLocation(p);
}
我在制定自己的解决方案时遇到了这个问题,我很高兴看到它完全按照你的描述工作,我想我会发布它。需要注意的是,我还使用了 2Way MVVM 绑定模式,它可以完美运行。你需要两件事:
1)此扩展方法可帮助在运行时查找作为 pin 的父级的 MapLayer:
public static T FindVisualParent<T>(this DependencyObject obj)
where T : DependencyObject
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
T typed = parent as T;
if (typed != null)
{
return typed;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
2) 在图钉上的 Dragging 事件处理程序中,调用该扩展方法以引用托管 MapLayer,然后触发最漂亮的 InvalidateArrange 方法(来自 UIElement),如下所示:
void ParentMap_MouseMove(object sender, MouseEventArgs e)
{
var map = sender as Microsoft.Maps.MapControl.Map;
var parentLayer = this.FindVisualParent<MapLayer>();
if (this.isDragging)
{
var mouseMapPosition = e.GetPosition(map);
var mouseGeocode = map.ViewportPointToLocation(mouseMapPosition);
this.Location = mouseGeocode;
parentLayer.InvalidateArrange();
}
}
这应该异步执行视觉更新,并在引脚拖动时为您提供一个很好的滑动行为。高温高压