Mahshid Fallah
Mahshid Fallah

Reputation: 15

Clear MapView layer in ArcGIS Runtime for Android

In my project, I use a MapView from ArcGIS Runtime for Android. I perform a query for features within a polygon and with a certain attribute value. When the search completes, I want to clear all existing graphics on the map. I tried map.removeAll() but it clears all of my layers! How can I clear all graphics instead of removing all layers?

This is my code:

private class AsyncQueryTask extends AsyncTask<String, Void, FeatureResult> {

    @Override
    protected void onPreExecute() {

        progress = new ProgressDialog(Naghshe.this);
        progress = ProgressDialog.show(Naghshe.this, "",
                "Please wait....query task is executing");

    }


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

        if (queryArray == null || queryArray.length <= 1)
            return null;

        String url = queryArray[0];
        QueryParameters qParameters = new QueryParameters();
        String whereClause = queryArray[1];
       // SpatialReference sr = SpatialReference.create(102100);
        qParameters.setGeometry(map.getExtent());
      //  qParameters.setOutSpatialReference(sr);
        qParameters.setReturnGeometry(true);
        qParameters.setWhere(whereClause);

        QueryTask qTask = new QueryTask(url);

        try {
            FeatureResult results = qTask.execute(qParameters);

            return results;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(FeatureResult results) {


        if (results != null) {
            int size = (int) results.featureCount();

            for (Object element : results) {
                progress.incrementProgressBy(size / 100);
                if (element instanceof Feature) {
                    Feature feature = (Feature) element;
                    Graphic graphic = new Graphic(feature.getGeometry(),
                            feature.getSymbol(), feature.getAttributes());

                    _graphicsLayer = new GraphicsLayer();
                    _graphicsLayer.addGraphic(graphic);

                    SimpleRenderer sr = new SimpleRenderer(new SimpleFillSymbol(Color.RED));
                    _graphicsLayer.setRenderer(sr);

                    boolean m =false;
                    if(!m)
                    {
                        map.addLayer(_graphicsLayer);
                        m=true;
                    }else {
                        map.removeLayer(_graphicsLayer);

                        map.addLayer(_graphicsLayer);
                    }

                    map.zoomin();

                    k++;
                }
            }
        }
        progress.dismiss();
        boolQuery = false;
    }

Upvotes: 1

Views: 717

Answers (1)

Gary Sheppard
Gary Sheppard

Reputation: 4932

MapView.removeAll() removes all layers from the map. That's not what you want.

Instead, try looping through the MapView's layers and for each layer of type GraphicsLayer (but not of type ArcGISFeatureLayer), either remove the layer or remove the graphics from the layer. Do something like this:

private void somewhereInYourExistingCode() {
    ...
    for (Layer layer : map.getLayers()) {
        clearGraphics(layer);
    }
    ...
}

private void clearGraphics(Layer layer) {
    /**
     * ArcGISFeatureLayer extends GraphicsLayer, but what you're
     * probably looking for is GraphicsLayer objects that are
     * not of type ArcGISFeatureLayer.
     */
    if (layer instanceof GraphicsLayer && !(layer instanceof ArcGISFeatureLayer)) {
        // You can call map.removeLayer(layer), or...
        ((GraphicsLayer) layer).removeAll();
    } else if (layer instanceof GroupLayer) {
        GroupLayer groupLayer = (GroupLayer) layer;
        for (Layer sublayer : groupLayer.getLayers()) {
            // Recursive call
            clearGraphics(sublayer);
        }
    }
}

Upvotes: 1

Related Questions