1

在我的项目中,我使用了MapViewArcGIS Runtime for 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;
    }
4

1 回答 1

1

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

相反,请尝试遍历MapView's 层,并为每个 type 层GraphicsLayer(但不是 type 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);
        }
    }
}
于 2017-05-31T11:40:11.320 回答