nqazi

spatial query based on mouse click event

Discussion created by nqazi on May 6, 2013
Latest reply on Sep 1, 2014 by HimBromBeere
Please look the code , I  wish to access the feature at the mouse coordinate on mouse click event. however my code below return all the records rather than a single point. any guess what is wrong.

IMxApplication pMxApp = null;
            pMxApp = (IMxApplication)ArcMap.Application;
            IMxDocument pMxDoc = null;

            pMxDoc = ArcMap.Document;
            if (pMxDoc.SelectedLayer == null)
            {
                MessageBox.Show("There is no layer selected.  First select a time-aware layer.");
                return;
            }
            IMap pMap = pMxDoc.FocusMap;
            IFeatureLayer feature_layer = (IFeatureLayer)pMap.get_Layer(0);// put the index of ur lauer
            IPoint point = new PointClass();

            x = pMxDoc.CurrentLocation.X; // this is point returns when user click on mouse.
            y = pMxDoc.CurrentLocation.Y;
            MessageBox.Show(x+ ":" + y);
            point.PutCoords(x, y);
            // point.PutCoords(280, 240);
            point.SpatialReference = pMap.SpatialReference;
            ISpatialFilter spatial_filter = new SpatialFilterClass();
            spatial_filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            spatial_filter.Geometry = point;
            spatial_filter.GeometryField = feature_layer.FeatureClass.ShapeFieldName;
            MessageBox.Show(spatial_filter.GeometryField.ToString());
            IFeatureCursor feature_cursor = feature_layer.FeatureClass.Search(spatial_filter, false);
            IFeature feature = feature_cursor.NextFeature();
         
            while (feature != null)   // here it returns the whole records rather than single point.
            {
                // do what u wan0t with the feature
                MessageBox.Show(feature.get_Value(3).ToString());
                feature = feature_cursor.NextFeature();
            }
        }

   
    }

Outcomes