1

我正在编写一个使用 GMapControl 的 WPF 程序。我想允许用户点击地图并在用户点击的地方添加一个标记。我不想使用“MouseUp”和“MouseDown”事件,以便仅在用户实际单击地图并忽略拖放时才会捕获该事件。另外,我希望能够以同样的方式捕捉手写笔和触摸事件。我注意到没有“点击”事件。对于如何做到这一点,还有其他最佳实践吗?

谢谢,

4

3 回答 3

1

似乎已经晚了,但我这样做的方式是创建一个集合,该集合将处理多边形到 MapControl 和事件的渲染。首先是创建一个可以扩展的多边形基类。

public abstract class BasePolygon : GMapPolygon
{
    public BasePolygon() : base(new List<PointLatLng>())
    {

    }

    public virtual void Render(Map map)
    {
        //code for displaying polygons on map goes here, basically
        map.Markers.Add(this);
    }

    public virtual void Derender(Map map)
    {
        //code for removing polygons on map goes here, basically
        map.Markers.Remove(this);
    }
}

然后创建一个集合,作为一个层来处理我们的多边形和它的事件。它的行为类似于具有属性和事件的ListBoxor 。ListViewSelectedItemSelectionChanged

public abstract class BasePolygonList : List<BasePolygon>
{
    private BasePolygon SelectedItem_;
    public BasePolygon SelectedItem 
    { 
        get
        {
            return this.SelectedItem_;
        }

        set
        {
            this.SelectedItem_ = value;

            //fire the event when a polygon is 'clicked'
            this.OnSelectionChanged();
        }
    }

    protected Map map;

    public BasePolygonList(Map map)
    {
        this.map = map;
    }

    //The Event which will fire if a polygon is clicked
    public event EventHandler SelectionChanged = delegate { };
    public void OnSelectionChanged()
    {
        if (this.SelectionChanged == null) return;

        this.SelectionChanged(this, new EventArgs());
    }

    //Render our polygons on the Map Control
    public void Render()
    {
        foreach(BasePolygon poly in this)
        {
            //Draw the polygon on the map
            poly.Render(this.map);

            //Enable the HitTest of the polygon
            poly.Shape.IsHitTestVisible = true;

            //Attach the Click Event at the polygon
            ((FrameworkElement)poly.Shape).MouseDown += (sender, e) =>
            {
                //Make sure that the Left Mouse Button is the one clicked 
                if(e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                    //Set the the current polygon on the foreach as the Selected item
                    //It will also fire the SelectionChanged event
                    this.SelectedItem = poly;
            };
        }
    }
}


示例使用

BasePolygonList polygonCollection = new BasePolygonList(MapControl);

//add your polygons here
//add your polygons here
//add your polygons here

//Display the polygons on the MapControl
polygonCollection.Render();

//do something when a polygon is clicked
polygonCollection.SelectionChanged += (s,e) =>
{
  Console.WriteLine("A polygon is Clicked/Selected");
  //get the object instance of the selected polygon
  BasePolygon SelectedPoly = polygonCollection.SelectedItem;
};


继承基类

您还可以继承BasePolygon该类以满足您的需求。例如

public class RealProperty : BasePolygon
{
    public string OwnerName { get; set; }
    public decimal Area { get; set; }
    public decimal MarketValue { get; set; }
}

子类的实现

BasePolygonList RealPropertyCollection = new BasePolygonList(MapControl);

//create our polygon with data
//don't forget to add the vertices
RealProperty RealPropertyItem1 = new RealProperty()
{
   OwnerName = "Some Owner Name",
   Area = 1000,
   MarketValue = 650000
};

//Add the created polygon to the list
RealPropertyCollection.Add(RealPropertyItem1);

//Display the polygons on the MapControl
RealPropertyCollection.Render();

//do something when a polygon is clicked
RealPropertyCollection.SelectionChanged += (s,e) =>
{
  //get the object instance of the selected polygon
  RealProperty SelectedPoly = (RealProperty)RealPropertyCollection.SelectedItem;

  //Display the data
  Console.WriteLine("Owner Name: " + SelectedPoly.OwnerName);
  Console.WriteLine("Area: " + SelectedPoly.Area);
  Console.WriteLine("MarketValue : " + SelectedPoly.MarketValue );
};
于 2018-04-24T17:44:09.647 回答
0

您可以从以下链接获得答案

点击 [这里] ( https://github.com/radioman/greatmaps )

注意 CustomMarkerDemo.xaml.cs 并将其添加到您的程序中。此自定义标记具有您需要的点击事件。

于 2016-09-02T09:09:22.693 回答
-1

Well, if there is no Click event, you're going to have to handle MouseDown + MouseUp to detect clicks. Just store the e.Position in MouseDown, and in MouseUp, compare to make sure the mouse hasn't moved much:

private Point downPoint;

private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
    downPoint = e.Position;
}

private void OnMouseUp(Object sender, MouseButtonEventArgs e)
{
    if (Math.Abs(downPoint.X - e.Position.X) < SystemParameters.MinimumHorizontalDragDistance &&
        Math.Abs(downPoint.Y - e.Position.Y) < SystemParameters.MinimumVerticalDragDistance)
    {
        HandleClick(sender, e);
    }
}

You'd need to do something similar for stylus and touch support.

于 2013-07-09T14:25:40.630 回答