需求:
ArcGIS Runtime SDK for Android 100 获取要素图层属性信息
示例数据:
链接:https://pan.baidu.com/s/1AS48LQiP-QnrnWTywQ13Aw
提取码:kjl8
测试版本:
ArcGIS Runtime SDK for Android 100.10
示例代码:
package cn.geoscene.myapplication0303;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final static String TAG = MainActivity.class.getSimpleName();
private MapView mMapView;
private ShapefileFeatureTable shapefileFeatureTable;
private FeatureLayer shapefileFeatureLayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create a new map to display in the map view with a streets basemap
mMapView = findViewById(R.id.mapView);
ArcGISMap map = new ArcGISMap(Basemap.createLightGrayCanvas());
mMapView.setMap(map);
// load the shapefile with a local path
shapefileFeatureTable = new ShapefileFeatureTable(
getExternalFilesDir(null) + getString(R.string.bj3857_path1));
// create a feature layer to display the shapefile
shapefileFeatureLayer = new FeatureLayer(shapefileFeatureTable);
// add the feature layer to the map
mMapView.getMap().getOperationalLayers().add(shapefileFeatureLayer);
shapefileFeatureTable.addDoneLoadingListener(() -> {
if (shapefileFeatureTable.getLoadStatus() == LoadStatus.LOADED) {
// zoom the map to the extent of the shapefile
mMapView.setViewpointAsync(new Viewpoint(shapefileFeatureLayer.getFullExtent()));
queryAttribute(shapefileFeatureLayer);
} else {
String error = "Shapefile feature table failed to load: " + shapefileFeatureTable.getLoadError().toString();
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
Log.e(TAG, error);
}
});
}
/**
* 属性查询
* @param featureLayer
*/
private void queryAttribute(FeatureLayer featureLayer) {
QueryParameters query = new QueryParameters();
query.setWhereClause("1=1");
final ListenableFuture<FeatureQueryResult> featureQueryResult
= featureLayer.getFeatureTable().queryFeaturesAsync(query);
featureQueryResult.addDoneListener(new Runnable() {
@Override
public void run() {
try {
List<Feature> mapQueryResult = new ArrayList<>();//查询统计结果
FeatureQueryResult result = featureQueryResult.get();
Iterator<Feature> iterator = result.iterator();
Feature feature;
int i=0;
while (iterator.hasNext()) {
feature = iterator.next();
mapQueryResult.add(feature);
i = i + 1;
Log.d("info", "i的值是: " + i);
Log.d("info", feature.getAttributes().toString());
}
Toast.makeText(MainActivity.this,"查询出"+mapQueryResult.size()+"个符合要求的结果",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.resume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.dispose();
}
}
上述设置查询条件query.setWhereClause("1=1");表示查询要素属性表所有属性信息。