How can I determine which point was clicked on?

734
2
Jump to solution
08-08-2017 05:59 PM
KayugaSolution
New Contributor

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?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
GregDeStigter
Esri Contributor

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.

View solution in original post

0 Kudos
2 Replies
GregDeStigter
Esri Contributor

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.

0 Kudos
KayugaSolution
New Contributor

Thanks for your help!

0 Kudos