MapView.SetExtent problem

3369
1
07-19-2015 10:56 PM
husseinemam1
New Contributor

I'm making a simple app to Query Features (Polygons) and then zoom to results , but when it zooms the zoom is too far (no envelope padding in the setExtent method ) , I barely see the feature , and it doesn't zoom more than this even with the manual double tap technique

Untitled.png

Here is the code :

public class MainActivity extends ActionBarActivity {

  MapView mMapView;

  ArcGISFeatureLayer mFeatureLayer;

  GraphicsLayer graphicsLayer;

   boolean mIsMapLoaded;

  String mFeatureServiceURL;

  EditText queryText;

   public void onSubmit(View view){

   //reference the values
   queryText = (EditText) findViewById(R.id.queryText);

   new QueryFeatureLayer().execute(queryText.getText().toString());

  }

   class QueryFeatureLayer extends AsyncTask<String, Void, FeatureResult> {

   @Override
   protected FeatureResult doInBackground(String... params) {

  String whereClause = "name ='" + params[0] + "'";

   // Define a new query and set parameters
   QueryParameters mParams = new QueryParameters();

  mParams.setWhere(whereClause);

  mParams.setReturnGeometry(true);

   // Define the new instance of QueryTask
   QueryTask queryTask = new QueryTask(mFeatureServiceURL);

  FeatureResult results;

   try {

   // run the querytask
   results = queryTask.execute(mParams);

   return results;

  } catch (Exception e) {

  e.printStackTrace();

  }

   return null;

  }

   @Override
   protected void onPostExecute(FeatureResult results) {

   super.onPostExecute(results);

   // Remove the result from previously run query task
   graphicsLayer.removeAll();

   // Define a new marker symbol for the result graphics
   SimpleMarkerSymbol sms = new SimpleMarkerSymbol(Color.BLUE, 10, SimpleMarkerSymbol.STYLE.CIRCLE);

  SimpleFillSymbol fsymbol = new SimpleFillSymbol(Color.RED);

   // Envelope to focus on the map extent on the results
   Envelope extent = new Envelope();

  // iterate through results
   for (Object element : results) {

   // if object is feature cast to feature
   if (element instanceof Feature) {

  Feature feature = (Feature) element;

   // convert feature to graphic
   Graphic graphic = new Graphic(feature.getGeometry(), feature.getSymbol(), feature.getAttributes());


   mMapView.setExtent(feature.getGeometry());

   // add it to the layer
   graphicsLayer.addGraphic(graphic);

  }

  }


   }

  }

   @Override
   protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

   final MapOptions mStreetsBasemap = new MapOptions(MapOptions.MapType.STREETS);

   // Retrieve the map and initial extent from XML layout
   mMapView = (MapView) findViewById(R.id.map);

   // Get the feature service URL from values->strings.xml
   mFeatureServiceURL = this.getResources().getString(R.string.featureServiceURL);

   // Add Feature layer to the MapView
   mFeatureLayer = new ArcGISFeatureLayer(mFeatureServiceURL, ArcGISFeatureLayer.MODE.ONDEMAND);

   mMapView.addLayer(mFeatureLayer);

   // Add Graphics layer to the MapView

   SimpleRenderer sr = new SimpleRenderer(

   new SimpleFillSymbol(Color.RED));

   graphicsLayer.setRenderer(sr);

   //mGraphicsLayer = new GraphicsLayer();
   mMapView.addLayer(graphicsLayer);

   //add the code below to set a listener that will be called when the MapView is initialized.
  // You will check this in a later step to see if the map is ready for a user to make queries.

   mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {

   public void onStatusChanged(Object source, STATUS status) {

   if ((source == mMapView) && (status == OnStatusChangedListener.STATUS.INITIALIZED)) {

   mIsMapLoaded = true;

  }

  }

  });

  }

}

0 Kudos
1 Reply
EricBader
Occasional Contributor III

Have you tried this?

  1. Collect all of the extents of the selected features into a Geometry[] array
  2. Use GeometryEngine to union all the extends into one Geometry
  3. Use the extent of the resulting Polygon as the new extent for your MapView.

That might be one way to do it.

You might also be able to call merge(), merging each Envelope with the next until you've merged them all.

I hope this helps!

0 Kudos