ThinkGeo.com    |     Documentation    |     Premium Support

Selecting feature on mouse click

I’m trying to figure out how to click on a feature on a map and “select” it.

I have multiple layers including two PersonalGeoDatabaseFeatureLayers, two ShapeFileFeatureLayers, and the TrackShapeLayer containing user drawn shapes. What’s the best way to loop through all of these to find the feature I clicked on?

private void wpfMap1_Click(object sender, MapClickWpfMapEventArgs e)
{
    Point point = new Point(e.WorldX, e.WorldY);

    InMemoryFeatureLayer selectLayer = (InMemoryFeatureLayer)Map1.FindFeatureLayer("SelectLayer");
    selectLayer.InternalFeatures.Clear();

    foreach (Layer layer in worldOverlay.Layers) //worldOverlay is the main overlay containing my layers
    {
         //code to find selected feature here
    }
}

I checked some examples on here but no luck. Thanks in advance.

Hi Dan,

In fact I think your logic should get enhancement.

Because the TrackOverlay should be a temp overlay just like EditOverlay, so it should have an InmemoryFeatureLayer to save the track result. I think the logic can like this: If the trackoverlay contains feature, you always select feature in it. Or you will find the feature in other overlay. Maybe you can directly ignore TrackOverlay.

As below is the code to find the selected feature in InmemoryFeatureLayer, you can also calculate the distance to make sure the feature in what you want:

        PointShape point;
        double distance = 50;

        InMemoryFeatureLayer layer = new InMemoryFeatureLayer();
        layer.Open();
        Collection<Feature> features = layer.QueryTools.GetFeaturesNearestTo(point, GeographyUnit.Meter, 1, ReturningColumnsType.AllColumns);
        
        foreach (Feature feature in features)
        {
            if (point.GetDistanceTo(feature, GeographyUnit.Meter, DistanceUnit.Meter) < distance)
            {
                // feature is what you want
            }
        }

You can handle PersonalGeoDatabaseFeatureLayer and ShapeFileFeatureLayer can be handle just the same logic.

Wish that’s helpful.

Regards,

Ethan