0

我在地图中添加了两个要素图层,当我显示 MapView 时可以看到它们。但是,只有最后一个添加的要素图层可以由 Touch Listener 处理。我无法弄清楚如何让 Touch Listener 考虑所有功能层。

我的目标是将点击 CARTO_ETARE 的功能与点击 CARTO_PT_EAU 的功能区分开来。

任何帮助,将不胜感激。

mGeodatabase = new Geodatabase(mGeoDb);
// load the geodatabase
mGeodatabase.loadAsync();
// add feature layer from geodatabase to the ArcGISMap
mGeodatabase.addDoneLoadingListener(new Runnable() {
    @Override
    public void run() {
        for (GeodatabaseFeatureTable geoDbTable : mGeodatabase.getGeodatabaseFeatureTables()){

            ArrayList<String> list_of_tables = new ArrayList<String>();
            list_of_tables.add("CARTO_ETARE");
            list_of_tables.add("CARTO_PT_EAU");
            Set<String> set = new HashSet<String>(list_of_tables);
            if (set.contains(geoDbTable.getTableName())) {

                mFeatureLayer = new FeatureLayer(geoDbTable);
                mFeatureLayer.setLabelsEnabled(true);
                mFeatureLayer.setSelectionWidth(10);

                //featureLayer.selectFeatures();
                mMap.getOperationalLayers().add(mFeatureLayer);
                mMapView.setMap(mMap);

                // set an on touch listener to listen for click events
                mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(MainActivity.this, mMapView) {
                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e) {
                        // get the point that was clicked and convert it to a point in map coordinates
                        Point clickPoint = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                        int tolerance = 10;
                        double mapTolerance = tolerance * mMapView.getUnitsPerDensityIndependentPixel();

                        // create objects required to do a selection with a query
                        Envelope envelope = new Envelope(clickPoint.getX() - mapTolerance,
                                clickPoint.getY() - mapTolerance,
                                clickPoint.getX() + mapTolerance,
                                clickPoint.getY() + mapTolerance,
                                mMap.getSpatialReference());
                        QueryParameters query = new QueryParameters();
                        query.setGeometry(envelope);

                        // call select features
                        mFuture = mFeatureLayer.selectFeaturesAsync(query, FeatureLayer.SelectionMode.ADD);
                        // add done loading listener to fire when the selection returns
                        mFuture.addDoneListener(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    //call get on the future to get the result
                                    FeatureQueryResult result = mFuture.get();
                                    // create an Iterator
                                    Iterator<Feature> iterator = result.iterator();
                                    Feature feature;
                                    while (iterator.hasNext()) {
                                        feature = iterator.next();
                                        Map<String, Object> attributes = feature.getAttributes();
                                                                                    if(feature.getFeatureTable().getTableName().equals("CARTO_PT_EAU")) {
                                            Toast.makeText(getApplicationContext(), Long.toString((Long)attributes.get("ID_PT_EAU")), Toast.LENGTH_SHORT).show();
                                        }
                                        else if(feature.getFeatureTable().getTableName().equals("CARTO_ETARE")) {
                                            mPdfFilename = (String) attributes.get("COD"); 
                                        }
                                    }
                                } catch (Exception e) {
                                    Log.e(getResources().getString(R.string.app_name), "Select feature failed: " + e.getMessage());
                                }
                            }
                        });
                        return super.onSingleTapConfirmed(e);
                    }
                });
            }

        }
    }
});
4

1 回答 1

0

您当前的代码将仅选择此处的每行 mFeatureLayer 中的任何功能

mFuture = mFeatureLayer.selectFeaturesAsync(query, FeatureLayer.SelectionMode.ADD);

每次在地理数据库表上的循环中都会被覆盖,因此它只是保存最后添加的任何图层。

我认为识别或重载之一可能更适合您的目的。

一些快速示例使用代码是:

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
  android.graphics.Point screenPoint = new android.graphics.Point((int) e.getX(), (int) e.getY());

  final ListenableFuture<List<IdentifyLayerResult>> identifyFuture = mMapView
      .identifyLayersAsync(screenPoint, 10, false, 10);
  identifyFuture.addDoneListener(new Runnable() {
    @Override public void run() {
      try {
        List<IdentifyLayerResult> identifyLayerResultList = identifyFuture.get();
        //if the point that the identify occurs at contains points from both layers, this should return 2 results, one for each layer
        Log.d("identifySize", String.valueOf(identifyLayerResultList.size()));
        for (IdentifyLayerResult result : identifyLayerResultList) {
          LayerContent layer = result.getLayerContent();
          //do whatever you want for the layer
          //the features are retrieved through result.getElements()

        }
      } catch (InterruptedException | ExecutionException e1) {
        e1.printStackTrace();
      }
    }
  });
  return super.onSingleTapConfirmed(e);
}
于 2017-09-18T21:16:36.253 回答