如何使用WPF的GMap.net响应单击事件 [英] How to respond to click event with GMap.net for WPF

查看:273
本文介绍了如何使用WPF的GMap.net响应单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用GMapControl的WPF程序.我想允许用户单击地图,并在用户单击的位置添加标记.我不想使用"MouseUp"和"MouseDown"事件,以便仅在用户实际点击地图并忽略拖放时才捕获该事件.另外,我希望能够以相同的方式捕获手写笔并触摸事件.我注意到没有点击"事件.是否还有其他最佳做法,该怎么做?

I am writing a WPF program that uses GMapControl. I would like to allow the user to click on the map and add a marker where the user have clicked. I do not want to use the "MouseUp" and "MouseDown" events so that the event will only be caught when the user actually clicks the map and ignore drag and drop. Also, I would like to be able to catch stylus and touch events the same way. I noticed that there is no "click" event. Is there any other best practice to how this should be done?

Thnx,

推荐答案

似乎已经很晚了,但是我要做的是创建一个集合,该集合将处理多边形向MapControl和Events的渲染.首先是创建可以扩展的多边形的基类.

It seems to be late but the way I did it is to create a collection which will handle the rendering of the polygons to the MapControl and the Events. First is to create a base class our polygons which could be extended.

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

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

And then create the collection which would act as a Layer that handle our polygons and it's events. It behaves like a ListBox or ListView which had the SelectedItem property and SelectionChanged event.

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类以适合您的需求.例如

You could also inherit the BasePolygon class to suit your needs. For example

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

这篇关于如何使用WPF的GMap.net响应单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆