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.

I am trying to create an application, where in I want to zoom to selected features. Basically, I want to zoom to the extent of a selected point or polygon.

share|improve this question
    
I am trying to do this in javascript. –  shipi Mar 2 '12 at 18:55
add comment

3 Answers 3

Try this will work for you

function ZoomToSelection() {
      graphics = new esri.Graphic();
      var taxLotExtent;
  graphics = featureLayer.getSelectedFeatures();
      taxLotExtent = graphics[0].geometry.getExtent();
  map.setExtent(taxLotExtent);
  }
share|improve this answer
    
The above one is for single feature for multiple features use esri.geometry.Extent(pass the individual geometry extent by iterating). You will get the resultant Extent of the selected fetures. –  Jagadesh Mar 12 '13 at 13:40
add comment

I suspect the java is not far from this (this is C#) but the principles are the same. Query the selection extents, union them, bump the result by a factor, and show it.

// passing in ActiveView 
// passing in targetLayer
// ZOOM_FACTOR = 1.5 
// ensure we have selected features
IFeatureSelection featureSelection = targetLayer as IFeatureSelection;
if (featureSelection == null) return;

ISelectionSet selectionSet = featureSelection.SelectionSet;

using (ComReleaser comReleaser = new ComReleaser())
{
    // search for each selection
    ICursor cursor;
    selectionSet.Search(null, true, out cursor);
    comReleaser.ManageLifetime(cursor);

    // build an envelope containing a union of the selections
    IFeatureCursor featureCursor = cursor as IFeatureCursor;
    IFeature feature;
    IEnvelope envelope = new EnvelopeClass();
    while ((feature = featureCursor.NextFeature()) != null)
    {
        IGeometry geometry = feature.Shape;
        IEnvelope featureExtent = geometry.Envelope;
        envelope.Union(featureExtent);
    }

    if (!envelope.IsEmpty)
    {
        // set the extent
        envelope.Expand(ZOOM_FACTOR, ZOOM_FACTOR, true);
        view.Extent = envelope;
        view.Refresh();
    }
}
share|improve this answer
add comment

You should be able to do this by calling getSelectedFeatures() on your Feature Layer (big assumption that this is a graphics or feature layer, am i correct here?) and then iterating through the returned array of Graphic objects. If the features are polygons, then the geometry property will have a getExtent() method. Just keep track of the extreme lat/lng values and set the map extent to these once you've checked all of the selected features. I'm a little confused by your mention of 'point and polygon' because I thought feature classes had to be one type or the other. Are you trying to zoom in on a group of features selected from two different sets? This would be a little trickier but should still be achievable by a similar method.

share|improve this answer
add comment

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.