如何使用GMap.net for WPF响应click事件

时间:2013-07-09 13:45:36

标签: wpf c#-4.0 gmap.net

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

Thnx,

3 个答案:

答案 0 :(得分: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);
    }
}

然后创建一个集合,它将充当处理我们的多边形及其事件的图层。它的行为类似于具有ListBox属性和ListView事件的SelectedItemSelectionChanged

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 BaseFactory(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 );
};

答案 1 :(得分:0)

您可以通过以下链接获得答案

点击[此处](https://github.com/radioman/greatmaps

观察CustomMarkerDemo.xaml.cs并将其添加到您的程序中。此自定义标记包含您需要的点击事件。

答案 2 :(得分:-1)

好吧,如果没有Click事件,您将需要处理MouseDown + MouseUp来检测点击次数。只需将e.Position存储在MouseDown中,然后在MouseUp中进行比较,以确保鼠标移动不多:

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);
    }
}

你需要为手写笔和触控支持做类似的事情。