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、效果: