Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
so feature is not selected in the map.right? –  vinayan Jan 10 '13 at 7:59
    
Do you want to select the feature and highlight it or just zoom the map? –  Emi Jan 10 '13 at 8:36
    
I want to zoom that feature which i selected in ListBox –  Jagadesh Jan 10 '13 at 8:39
    
@Jagadesh - please post where you are going wrong.. –  vinayan Jan 10 '13 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); –  Emi Jan 10 '13 at 11:10
show 1 more comment

4 Answers 4

up vote 2 down vote accepted

This is not tested.

public void ZoomTo(IGeometry geometry)
{
    IMxDocument doc = (IMxDocument)m_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 = 1/100; //set to 1:100 scale          
    }
    else
    {
        pEnvelope = geometry.Envelope;
        pEnvelope.Expand(1.2, 1.2, true);

        pActiveView.Extent = pEnvelope;
    }

    pActiveView.Refresh();
}
share|improve this answer
    
Thankyou it worked fine –  Jagadesh Jan 10 '13 at 11:54
    
Now i want to zoom to some collection of features where all are point features then how to work with it. –  Jagadesh Jan 16 '13 at 12:09
    
check snipplr.com/view/48443 –  vinayan Jan 16 '13 at 12:32
add comment

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)
share|improve this answer
    
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 –  Jagadesh Jan 10 '13 at 8:51
    
I saw that, any specific reason for it...do you intend to re-use it later? –  ujjwalesri Jan 10 '13 at 8:52
    
if the Geometry is an IPoint, it may not zoom..you might have to apply a buffer i think.. –  vinayan Jan 10 '13 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. –  blah238 Jan 10 '13 at 8:56
    
@vinayan yes it is point feature how to zoom now –  Jagadesh Jan 10 '13 at 9:10
show 3 more comments

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

share|improve this answer
    
HookAction is the only method to zoom ? because i am beginner and not aware of hookactions –  Jagadesh Jan 10 '13 at 9:28
1  
There are at least 3 ways to skin this cat... I am just offering one more. –  blah238 Jan 10 '13 at 9:34
add comment

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.

share|improve this answer
    
I want to zoom that feature in Map at minimum map scale –  Jagadesh Jan 10 '13 at 8:56
    
@Jagadesh what do you mean by "minimum map scale"? Also, no such requirement was not stated up front (a bit unfair). –  blah238 Jan 10 '13 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. –  Jagadesh Jan 10 '13 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? –  blah238 Jan 10 '13 at 9:04
    
(e.g. 1:2) so that we can see only that feature –  Jagadesh Jan 10 '13 at 9:07
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.