天天看點

ArcGIS Runtime SDK For Android 10.2.x版本空間查詢和屬性查詢

引言

arcgis查詢包括空間查詢和屬性查詢,其中空間查詢包括點選查詢、多邊形查詢等。

一、空間查詢

1、點選查詢

通過重寫MapView的OnSingleTapListener事件,對地圖點選事件進行處理。主要有三種實作方式:(1)根據點選的x、y坐标,直接調用FeatureLayer的getFeatureIDs(float x, float y, int tolerance)方法查詢得到要素的objectid;(2)編寫QueryParameters查詢參數,設定其查詢geometry為目前點,調用FeatureLayer的selectFeatures方法或者FeatureTable的queryFeatures方法進行查詢;(3)結合Query和QueryTask進行查詢,但該方式隻适合地圖服務的查詢。具體代碼如下:

mMapView.setOnSingleTapListener(new OnSingleTapListener() {
    @Override
    public void onSingleTap(float x, float y) {
        if (!mMapView.isLoaded())
            return;

        Point identifyPoint = mMapView.toMapPoint(x, y);

        try {
            highLightLayer.removeAll();

            Layer[] mapLayers = mMapView.getLayers();
            for (int i = mapLayers.length; i > 0; --i) {
                Layer item = mapLayers[i - 1];
                if (item instanceof FeatureLayer) {
                    FeatureLayer featureLayer = ((FeatureLayer) item);
                    //方式一
                    /*long[] objectIDs = featureLayer.getFeatureIDs(x, y, 10);

                    if (objectIDs != null && objectIDs.length > 0) {
                        Feature feature = featureLayer.getFeature(objectIDs[0]);//取第一個選中的要素
                        renderFeature(mMapView, feature, highLightLayer);
                        //initMapViewCallout(feature, identifyPoint);
                        break;
                    }*/

                    //方式二
                    QueryParameters queryParameters = new QueryParameters();
                    queryParameters.setGeometry(identifyPoint);
                    queryParameters.setReturnGeometry(true);
                    //queryParameters.setWhere("1=1");
                    //queryParameters.setSpatialRelationship(SpatialRelationship.INTERSECTS);
                    //SpatialReference sr = SpatialReference.create(102100);
                    //queryParameters.setInSpatialReference(featureLayer.getSpatialReference());
                    //queryParameters.setOutSpatialReference(sr);
                    //queryParameters.setOutFields(new String[]{"*"});
                    featureLayer.selectFeatures(queryParameters, FeatureLayer.SelectionMode.NEW, new CallbackListener<FeatureResult>() {
                        @Override
                        public void onCallback(FeatureResult result) {
                            if (result != null) {
                                Iterator<Object> it = result.iterator();
                                while (it.hasNext()) {
                                    Feature feature = (Feature) it.next();
                                    try {
                                        //featureLocated(feature);
                                        renderFeature(mMapView, feature, highLightLayer);
                                        break;
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(Throwable throwable) {

                        }
                    });
                    /*featureLayer.getFeatureTable().queryFeatures(queryParameters, new CallbackListener<FeatureResult>() {
                        @Override
                        public void onError(Throwable e) {
                            Log.e("Test", "Unable to perform query", e);
                        }

                        @Override
                        public void onCallback(FeatureResult result) {
                            if (result != null) {
                                Iterator<Object> it = result.iterator();
                                while (it.hasNext()) {
                                    Feature feature = (Feature) it.next();
                                    try {
                                        //featureLocated(feature);
                                        renderFeature(mMapView, feature, highLightLayer);
                                        break;
                                    } catch (Exception e) {ArcGIS Android API 應用開發之圖形繪制與長度面積量算
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    });*/
                }
            }
        } catch (Exception ex) {
            Log.i("test", ex.getMessage());
        }
    }
});
           

2、多邊形查詢

首先需要繪制一個多邊形,繪制多邊形的代碼可以參考ArcGIS Android API應用開發之圖形繪制與長度面積量算。通過繪制得到geometry之後,即可用點選查詢中的方法2進行查詢。

二、屬性查詢

類似點選查詢的方式二,隻不過需要手動設定where條件,例如:name = '項目1'等。一般查詢到要素之後,需要将該要素定位到地圖視圖的中部,此時可以調用featurelocated方法,見三。

三、上文用到的函數

/**
* 在這裡我們進行要素的高亮顯示,也就是要素渲染工作    *
*
* @param feature 要渲染的要素
* @return 傳回要素的中心點位置
*/
private Point renderFeature(MapView mMapView, Feature feature, GraphicsLayer highLightLayer) {
    highLightLayer.removeAll();
    Geometry geometry = feature.getGeometry();
    String typeName = geometry.getType().name();
    Envelope env = new Envelope();
    geometry.queryEnvelope(env);

    Graphic highlightGraphic = null;
    int color = Color.rgb(19, 255, 234);
    if (typeName.equalsIgnoreCase("point")) {
        SimpleMarkerSymbol sms = new SimpleMarkerSymbol(color, 20, SimpleMarkerSymbol.STYLE.CIRCLE);
        highlightGraphic = new Graphic(geometry, sms);
    } else if (typeName.equalsIgnoreCase("polyline")) {
        SimpleLineSymbol sls = new SimpleLineSymbol(color, 5);
        highlightGraphic = new Graphic(geometry, sls);
    } else if (typeName.equalsIgnoreCase("polygon")) {
        SimpleFillSymbol sfs = new SimpleFillSymbol(color);
        sfs.setAlpha(0);
        sfs.setOutline(new SimpleLineSymbol(color, 5));
        highlightGraphic = new Graphic(geometry, sfs);
    }

    highLightLayer.setMaxScale(mMapView.getMaxScale());
    highLightLayer.setMinScale(mMapView.getMinScale());
    highLightLayer.addGraphic(highlightGraphic);
    //mMapView.centerAt(env.getCenter(), true);
    return env.getCenter();
}
           
/**
  
  
   * 根據項目對象定位項目要素
  
  
   */

  
  
   private void featureLocated(Feature feature) {
  
  
       //點選清單中項目行,定位到地圖中的項目資料,并高亮顯示
  
  

  
  
       if (feature != null) {
  
  
           highLightLayer.removeAll();
  
  
           Point mapPoint = renderFeature(mMapView, feature, highLightLayer);
  
  
           Point pt = mMapView.toScreenPoint(mapPoint);//渲染圖層,并取得渲染點螢幕位置
  
  
           pt.setY(pt.getY() - 100);
  
  
           mMapView.centerAt(mMapView.toMapPoint(pt), true);
  
  
           initMapViewCallout(feature, mapPoint);
  
  
       }
  
  
   }
  
           

參考資料:http://blog.csdn.net/vaecer/article/details/8814071。