问题
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,
回答1:
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);
}
}
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 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;
};
}
}
}
Example Use
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;
};
Inheriting the Base Class
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; }
}
Implementation of the Child Class
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 );
};
回答2:
You can get your answer from the following link
Click [here] (https://github.com/radioman/greatmaps)
Watch for CustomMarkerDemo.xaml.cs and add this to your program. This custom marker has click event required by you.
回答3:
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.
来源:https://stackoverflow.com/questions/17549857/how-to-respond-to-click-event-with-gmap-net-for-wpf