FeatureLayer是要素图层,也是Arcgis的主要图层。用这个图层可以加载本地的shp文件。下面我们看怎样加载shp文件到MapView中。查看ArcGis API可知FeatureLayer的构造方法要传一个FeatureTable,而FeatureTable可用子类来实现。代码如下,其中shpPath为shp文件的本地路径。\
- private void initLayer() {
- try {
- featureLayer = new FeatureLayer(new ShapefileFeatureTable(
- shpPath));
- featureLayer.setRenderer(new SimpleRenderer(new SimpleFillSymbol(
- Color.GREEN)));
- mapView.addLayer(featureLayer);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
效果图如下。
这样就可以很简单地加载shp文件了。下面我们来学习一下FeatureLayer的查询。
一、FeatureLayer点击查询主要用到的方法是featureLayer.getFeatureIDs(float x, float y, int tolerance),方法与上一讲GraphicsLayer的查询差不多。可以获取每个要素的属性属性值。
- private void handleSingleTap(float x, float y) {
- long[] featureIds = featureLayer.getFeatureIDs(x, y, 8);
- if (featureIds != null && featureIds.length > 0) {
- for (long id : featureIds) {
- com.esri.core.map.Feature feature = featureLayer.getFeature(id);
- Map<String, Object> attrs = feature.getAttributes();
- Set<Entry<String, Object>> setEntry = attrs.entrySet();
- for (Entry<String, Object> entryItem : setEntry) {
- Log.e(TAG,
- entryItem.getKey() + "====" + entryItem.getValue());
- }
- }
- }
- }
二、利用FeatureTable查询,这是FeatureLayer特别的地方。比如在一个shp文件中,有多个要素,我们知道有每个要素中有name这个属性,怎样快速查询name为shanghai的要素呢?要用到FeatureTable的queryFeatures(QueryParameters query, CallbackListener<FeatureResult> callback)方法或者是queryIds(QueryParameters query, CallbackListener<long[]> callback)这个方法。
- private void queryTable() {
- FeatureTable featureTable = featureLayer.getFeatureTable();
- QueryParameters parameters = new QueryParameters();
- //查询条件,有点像数据库中的查询
- parameters.setWhere("name = 'shanghai'");<span style="white-space:pre"> </span>Future<long[]> queryIds = featureTable.queryIds(parameters,new CallbackListener<long[]>() {
- @Override
- public void onError(Throwable arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onCallback(long[] arg0) {
- // TODO Auto-generated method stub
- }
- });
- try {
- long[] Ids = queryIds.get();
- if (Ids != null && Ids.length > 0) {
- for (int i = 0; i < Ids.length; i++) {
- Feature feature = featureLayer.getFeature(Ids[i]);
- Log.i(TAG, feature.getAttributes().get("name") + "");
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
这样就可以快速查询到某个具体的要素。
Code for all