arcgis for android(九)地理查询 + 触摸事件

1、上一章讲了arcgis for android 入门与提高(八)加载shape和影像,arcgis for android 入门与提高(八)加载shape和影像_郝大大的博客-CSDN博客,这节讲如何查询shape自带的属性,用消息框显示出来。

2、本节内容用到了上节讲的加载shape,我们把数据加载进来,重写触摸的单击事件和长按事件,当单击的时候清除上次查询的内容,当长按图斑的时候显示属性值:

定义全局变量:

private FeatureLayer mFeatureLayer;
private Callout mCallout;

 导入数据:

private void importShape(String shapeFile) {
    ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(shapeFile);
    mFeatureLayer = new FeatureLayer(shapefileFeatureTable);
    //mFeatureLayer.setSelectionWidth(2);
    //mFeatureLayer.setSelectionColor(Color.RED);
    mMapView.getMap().getOperationalLayers().add(mFeatureLayer);
    shapefileFeatureTable.addDoneLoadingListener(new Runnable() {
        @Override
        public void run() {
            if (shapefileFeatureTable.getLoadStatus() == LoadStatus.LOADED) {
                mMapView.setViewpointGeometryAsync(shapefileFeatureTable.getExtent());
                String geoType = shapefileFeatureTable.getGeometryType().toString();
                SimpleRenderer renderer = null;
                SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.GREEN, 6);
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 1);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.argb(70, 252, 241, 144), lineSymbol);
                if (geoType.equals("POINT")) {
                    renderer = new SimpleRenderer(markerSymbol);
                }
                if (geoType.equals("POLYLINE")) {
                    renderer = new SimpleRenderer(lineSymbol);
                }
                if (geoType.equals("POLYGON")) {
                    renderer = new SimpleRenderer(fillSymbol);
                }
                mFeatureLayer.setRenderer(renderer);
            } else {
                String error = "Shapefile feature table failed to load: " + shapefileFeatureTable.getLoadError().toString();
                Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
            }
        }
    });
}

 触摸事件:

String shapeFile = Environment.getExternalStorageDirectory() + "/test.shp";
importShape(shapeFile);
mCallout = mMapView.getCallout();
mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
    //单击事件
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        if (mCallout.isShowing()) {
            mCallout.dismiss();
        }
        mFeatureLayer.clearSelection();
        return super.onSingleTapConfirmed(e);
    }

    //长按事件
    @Override
    public void onLongPress(MotionEvent e) {
        super.onLongPress(e);

        android.graphics.Point screenPoint = new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY()));
        Point clickPoint = mMapView.screenToLocation(screenPoint);// 屏幕坐标转地理坐标

        int tolerance = 10;
        double mapTolerance = tolerance * mMapView.getUnitsPerDensityIndependentPixel();
        Envelope envelope = new Envelope(clickPoint.getX() - mapTolerance, clickPoint.getY() - mapTolerance,
                clickPoint.getX() + mapTolerance, clickPoint.getY() + mapTolerance, mMapView.getSpatialReference());
        QueryParameters query = new QueryParameters();
        query.setGeometry(envelope);// 设置查询范围
        final ListenableFuture<FeatureQueryResult> featureQueryResultFuture = mFeatureLayer.selectFeaturesAsync(query, FeatureLayer.SelectionMode.NEW);

        featureQueryResultFuture.addDoneListener(new Runnable() {
            @Override
            public void run() {
                try {
                    FeatureQueryResult featureQueryResult = featureQueryResultFuture.get();
                    // create a textview to display field values
                    TextView calloutContent = new TextView(getApplicationContext());
                    calloutContent.setTextColor(Color.BLACK);
                    calloutContent.setSingleLine(false);
                    calloutContent.setVerticalScrollBarEnabled(true);
                    calloutContent.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
                    calloutContent.setMovementMethod(new ScrollingMovementMethod());
                    calloutContent.setLines(5);
                    Iterator<Feature> iterator = featureQueryResult.iterator();
                    Feature feature;
                    while (iterator.hasNext()) {
                        feature = iterator.next();
                        // create a Map of all available attributes as name value pairs
                        Map<String, Object> attr = feature.getAttributes();
                        Set<String> keys = attr.keySet();
                        for (String key : keys) {
                            Object value = attr.get(key);
                            calloutContent.append(key + ":" + value.toString()+ "\n");
                        }
                        // center the mapview on selected feature
                        Envelope envelope = feature.getGeometry().getExtent();
                        mMapView.setViewpointGeometryAsync(envelope, 200);
                        // show callout
                        mCallout.setLocation(envelope.getCenter());
                        mCallout.setContent(calloutContent);
                        mCallout.show();
                    }
                } catch (Exception e1) {
                    String error = "Select feature failed: " + e1.getMessage();
                    Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();

                }
            }
        });

    }


});

3、效果:

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值