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.
-
I am trying to do this in javascript. – shipi Mar 02 '12 at 18:55
5 Answers
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);
}
- 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
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();
}
}
- 13,313
- 6
- 55
- 96
- 1,060
- 6
- 15
Why not use graphicUtils.graphicsExtent
Simply pass in the array of graphics and you will get the extent.
- 4,395
- 7
- 48
- 69
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.
- 186
- 7
To zoom at particular feature, you need a geometry of the feature.
var geometry = selectedFeature.geometry;
zoomToFeature: function (geometry) {
this.map.setExtent(geometry.getExtent());
};
- 76,800
- 56
- 247
- 389