I'm working with WPF. In the attached example I have a map with four points. I have a handler that gets called when the user clicks on one of the points. Since the point clicked on may have slightly different coordinates from the one that I used to display it, how can I tell which point is the one the user clicked on?
Solved! Go to Solution.
Hi Daniel,
I notice you're using a Multipoint to hold your graphics. Multipoint geometries are rarely used for this kind of purpose. It's better to just use separate MapPoint graphics as this makes it easier to tell them apart.
Here's a quick sample with your data that should give correct Identify results:
private readonly Envelope _extent =
new Envelope(-118.36, 33.81, -118.39, 33.90, SpatialReferences.Wgs84);
private List<Graphic> _schools = new List<Graphic>()
{
new Graphic(new MapPoint(-118.386499, 33.844753, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "Redondo High" } }),
new Graphic(new MapPoint(-118.389740, 33.873542, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "Mira Costa" } }),
new Graphic(new MapPoint(-118.362154, 33.895733, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "Lawndale" } }),
new Graphic(new MapPoint(-118.369451, 33.813457, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "South" } }),
};
private GraphicsOverlay _schoolsOveraly;
public MainWindow()
{
InitializeComponent();
MyMapView.Map = new Map(Basemap.CreateImagery());
MyMapView.Map.InitialViewpoint = new Viewpoint(_extent);
// Schools Graphics
var symbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Colors.Red, 10d);
_schoolsOveraly = new GraphicsOverlay() { Renderer = new SimpleRenderer(symbol) };
foreach (var school in _schools)
_schoolsOveraly.Graphics.Add(school);
MyMapView.GraphicsOverlays.Add(_schoolsOveraly);
MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
}
private async void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
var results = await MyMapView.IdentifyGraphicsOverlayAsync(
_schoolsOveraly, e.Position, 1d, false, 1);
var school = results.Graphics?.FirstOrDefault();
if (school != null)
MessageBox.Show(school.Attributes["Name"].ToString(), "Graphic Tapped");
}
Also note that reprojection isn't necessary for your graphics, that will happen on-the-fly.
Hope that helps.
Hi Daniel,
I notice you're using a Multipoint to hold your graphics. Multipoint geometries are rarely used for this kind of purpose. It's better to just use separate MapPoint graphics as this makes it easier to tell them apart.
Here's a quick sample with your data that should give correct Identify results:
private readonly Envelope _extent =
new Envelope(-118.36, 33.81, -118.39, 33.90, SpatialReferences.Wgs84);
private List<Graphic> _schools = new List<Graphic>()
{
new Graphic(new MapPoint(-118.386499, 33.844753, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "Redondo High" } }),
new Graphic(new MapPoint(-118.389740, 33.873542, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "Mira Costa" } }),
new Graphic(new MapPoint(-118.362154, 33.895733, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "Lawndale" } }),
new Graphic(new MapPoint(-118.369451, 33.813457, SpatialReferences.Wgs84),
new Dictionary<string, object> {{ "Name", "South" } }),
};
private GraphicsOverlay _schoolsOveraly;
public MainWindow()
{
InitializeComponent();
MyMapView.Map = new Map(Basemap.CreateImagery());
MyMapView.Map.InitialViewpoint = new Viewpoint(_extent);
// Schools Graphics
var symbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Colors.Red, 10d);
_schoolsOveraly = new GraphicsOverlay() { Renderer = new SimpleRenderer(symbol) };
foreach (var school in _schools)
_schoolsOveraly.Graphics.Add(school);
MyMapView.GraphicsOverlays.Add(_schoolsOveraly);
MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
}
private async void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
var results = await MyMapView.IdentifyGraphicsOverlayAsync(
_schoolsOveraly, e.Position, 1d, false, 1);
var school = results.Graphics?.FirstOrDefault();
if (school != null)
MessageBox.Show(school.Attributes["Name"].ToString(), "Graphic Tapped");
}
Also note that reprojection isn't necessary for your graphics, that will happen on-the-fly.
Hope that helps.
Thanks for your help!