Android 清除 ArcGIS Runtime 中的 MapView 图层

Clear MapView layer in ArcGIS Runtime for Android

在我的项目中,我使用 ArcGIS Runtime 中的 MapView 作为 Android。我对多边形内具有特定属性值的要素执行查询。搜索完成后,我想清除地图上所有现有图形。我试过 map.removeAll() 但它清除了我所有的图层!如何清除所有图形而不是删除所有图层?

这是我的代码:

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;
    }

MapView.removeAll() 从地图中移除所有图层。这不是你想要的。

相反,尝试遍历 MapView 的图层,并针对 GraphicsLayer 类型的每一层(但不是 ArcGISFeatureLayer 类型)移除图层或移除图形从层。做这样的事情:

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);
        }
    }
}