6

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.

kuro
  • 344
  • 1
  • 11
shipi
  • 147
  • 1
  • 2
  • 6

5 Answers5

6

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);
  }
Jagadesh
  • 292
  • 3
  • 13
  • 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
  • You can't just iterate since you will then zoom to each individual feature in succession. You need to merge all the extents then zoom to the resulting extent. – NenadK Feb 26 '16 at 01:03
2

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();
    }
}
CaptDragon
  • 13,313
  • 6
  • 55
  • 96
Chaz
  • 1,060
  • 6
  • 15
1

Why not use graphicUtils.graphicsExtent

Simply pass in the array of graphics and you will get the extent.

Sam007
  • 4,395
  • 7
  • 48
  • 69
1

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.

Ryan Twilley
  • 186
  • 7
0

To zoom at particular feature, you need a geometry of the feature.

var geometry = selectedFeature.geometry;
zoomToFeature: function (geometry) {
  this.map.setExtent(geometry.getExtent());
};

Esri documentation of extent

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389