android怎么通过点击查询离线shp属性字段

已邀请:

张佳期

赞同来自:

demo1.jpg
public class MainActivity extends AppCompatActivity {

private MapView mMapView;
private Callout mCallout;

// private ServiceFeatureTable mServiceFeatureTable;
private ShapefileFeatureTable shapefileFeatureTable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMapView = findViewById(R.id.mapView);
// create an ArcGISMap with a topographic basemap
final ArcGISMap map = new ArcGISMap();
// set the ArcGISMap to the MapView
mMapView.setMap(map);
// set a viewpoint
mMapView.setViewpoint(new Viewpoint(34.057386, -117.191455, 100000000));
// get the callout that shows attributes
mCallout = mMapView.getCallout();

// load the shapefile with a local path
shapefileFeatureTable = new ShapefileFeatureTable(
getExternalFilesDir(null) + getString(R.string.bj3857_path1));

// create the feature layer using the service feature table
final FeatureLayer featureLayer = new FeatureLayer(shapefileFeatureTable);
// add the layer to the map
map.getOperationalLayers().add(featureLayer);

// set an on touch listener to listen for click events
mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// remove any existing callouts
if (mCallout.isShowing()) {
mCallout.dismiss();
}
// get the point that was clicked and convert it to a point in map coordinates
final Point screenPoint = new Point(Math.round(e.getX()), Math.round(e.getY()));
// create a selection tolerance
int tolerance = 10;
// use identifyLayerAsync to get tapped features
final ListenableFuture<IdentifyLayerResult> identifyLayerResultListenableFuture = mMapView
.identifyLayerAsync(featureLayer, screenPoint, tolerance, false, 1);
identifyLayerResultListenableFuture.addDoneListener(() -> {
try {
IdentifyLayerResult identifyLayerResult = identifyLayerResultListenableFuture.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);
for (GeoElement element : identifyLayerResult.getElements()) {
Feature feature = (Feature) element;
// 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);
// format observed field value as date
if (value instanceof GregorianCalendar) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
value = simpleDateFormat.format(((GregorianCalendar) value).getTime());
}
// append name value pairs to text view
calloutContent.append(key + " | " + value + "\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) {
Log.e(getResources().getString(R.string.app_name), "Select feature failed: " + e1.getMessage());
}
});
return super.onSingleTapConfirmed(e);
}
});

}

@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}

@Override
protected void onResume() {
super.onResume();
mMapView.resume();
}

@Override
protected void onDestroy() {
super.onDestroy();
mMapView.dispose();
}
}
其实参考官网这个案例(https://developers.arcgis.com/ ... utes/)即可,官网中是点击查询的要素服务,将要素服务改为你移动端本地的shp即可。

要回复问题请先登录注册