3

By using the QueryFilter I am getting some features based on the query and I am placing that feature names in a listbox. When the user selects a particular name in the listbox that feature has to be zoomed to in the map.

Below is my code:

IQueryFilter pQueryFilter = new QueryFilterClass();
 pQueryFilter.WhereClause = textBox1.Text + textBox2.Text;
 ICursor pCursor = pFeatureClass.Search(pQueryFilter, true) as ICursor;
 IRow pRow = pCursor.NextRow();
while (pRow != null)
 {
 listBox2.Items.Add(pRow.get_Value(9).ToString());
 pRow = pCursor.NextRow();
 }

Here is what I tried:

IActiveView pActiveView = pMxDocument.ActiveView;
 IGeometry pGeometry = pFeature.Shape;
 IEnvelope pEnvelope = pGeometry.Envelope;
 pActiveView.Extent = pEnvelope.Expand(1.2, 1.2, true);

But I am unable to zoom.

Fezter
22k11 gold badges72 silver badges128 bronze badges
asked Jan 10, 2013 at 6:59
6
  • so feature is not selected in the map.right? Commented Jan 10, 2013 at 7:59
  • Do you want to select the feature and highlight it or just zoom the map? Commented Jan 10, 2013 at 8:36
  • I want to zoom that feature which i selected in ListBox Commented Jan 10, 2013 at 8:39
  • @Jagadesh - please post where you are going wrong.. Commented Jan 10, 2013 at 10:09
  • @Jagadesh I added some comment in my answer. may be you missed it. you need to pass false in the expand method's third argument and you have to refresh arcmap's active view. For experimental please use pEnvelope.Expand(10, 10, false); Commented Jan 10, 2013 at 11:10

4 Answers 4

6

This is not tested.

public void ZoomTo(IGeometry geometry)
{
 IMxDocument doc = (IMxDocument)ArcMap.Application.Document;
 IMap map = (IMap)doc.FocusMap;
 IActiveView pActiveView = (IActiveView)map; 
 IEnvelope pEnvelope;
 if (geometry is IPoint)
 {
 IEnvelope currentEnv = pActiveView.Extent;
 IPoint point = (IPoint)geometry;
 currentEnv.CenterAt(point);
 pActiveView.Extent = currentEnv;
 map.MapScale = 100; //to set the scale to 1:100 
 }
 else
 {
 pEnvelope = geometry.Envelope;
 pEnvelope.Expand(1.2, 1.2, true);
 pActiveView.Extent = pEnvelope;
 }
 pActiveView.Refresh();
}
answered Jan 10, 2013 at 10:25
3
  • Now i want to zoom to some collection of features where all are point features then how to work with it. Commented Jan 16, 2013 at 12:09
  • check snipplr.com/view/48443 Commented Jan 16, 2013 at 12:32
  • 1
    If the geometry coordinate system is geographic and geometry type isn't point, then pEnvelope.Expand(1.2, 1.2, true); will zoom to a very big extent. Commented May 2, 2015 at 14:19
2

Try the following (assuming names are unique):

  1. Store the feature reference returned by pFeatureClass.Search(pQueryFilter, true).NextFeature() in a hashtable (data structure is not important), with key=name & value=actual feature object
  2. On the selection changed event for the list, call
    pActiveView.Extent=(hashtable[SelectedListItem] as IFeature).Extent
  3. This should zoomt to the feature (but wont select it upon zooming)
answered Jan 10, 2013 at 8:42
8
  • I slightly changed my code instead of ICursor i have taken IFeatureCursor from that i am getting IFeature i tried but its not zooming to the feature Commented Jan 10, 2013 at 8:51
  • I saw that, any specific reason for it...do you intend to re-use it later? Commented Jan 10, 2013 at 8:52
  • if the Geometry is an IPoint, it may not zoom..you might have to apply a buffer i think.. Commented Jan 10, 2013 at 8:55
  • You could store the feature reference directly in the ListView using its ListViewItem.Tag property. Just be sure to use a non-recycling cursor. Commented Jan 10, 2013 at 8:56
  • @vinayan yes it is point feature how to zoom now Commented Jan 10, 2013 at 9:10
1

You could also use the IHookActions interface for this, e.g.:

hookActions.DoAction(feature.Shape, esriHookActions.esriHookActionsZoom)

See also this sample: Using HookActions in custom commands

answered Jan 10, 2013 at 8:50
6
  • HookAction is the only method to zoom ? because i am beginner and not aware of hookactions Commented Jan 10, 2013 at 9:28
  • 2
    There are at least 3 ways to skin this cat... I am just offering one more. Commented Jan 10, 2013 at 9:34
  • This is only applicable to ArcEngine and no the Desktop Commented May 2, 2015 at 14:20
  • @FaridCher Are you sure? I remember testing it with ArcMap and it worked fine. Commented May 4, 2015 at 19:52
  • I haven't tested this with Desktop. But If you open IHookActions documentation (your own link), you will only see Arc Engine under Product Availability! Commented May 4, 2015 at 21:26
1

you just have to find out which feature the user selected.

ADDITION: May be for point feature the extent is so small that map can not zoom that much. So you need to make an envelope of that point and expand it as much as the map needs.

For point features:

IGeometry pointGeo = selectedFeature.Shape;
IEnvelope envelope = pointGeo.Envelop;
envelope.Expand(10,10,false);
map.ActiveView.Extent = envelope;
map.ActiveView.Refresh();

see more fore IEnvelope.Expand Method. This should work.

answered Jan 10, 2013 at 8:51
17
  • I want to zoom that feature in Map at minimum map scale Commented Jan 10, 2013 at 8:56
  • @Jagadesh what do you mean by "minimum map scale"? Also, no such requirement was not stated up front (a bit unfair). Commented Jan 10, 2013 at 8:58
  • For a particular Layer in a Map there are huge amount of Features placed. By querying particular fields i want to zoom that particular feature which user selected. Commented Jan 10, 2013 at 9:04
  • Okay... so you want to zoom to a feature. We got that part. What do you mean by "minimum map scale" is there a minimum scale level (e.g. 1:1200) that you don't want to automatically zoom below? Commented Jan 10, 2013 at 9:04
  • (e.g. 1:2) so that we can see only that feature Commented Jan 10, 2013 at 9:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.