在100.3中仍然使用FeatureLayer来加载geodatabase中的要素。
不过与10.2.9不同的是需要异步加载。
加载离线数据 geodatabase文件:
String path=StorageUtil.getSDCardRootPath(getApplicationContext())+"/ydyzt/data/GD_TDYT.geodatabase";
final Geodatabase localGdb=new Geodatabase(path);
// 异步加载
localGdb.loadAsync();
localGdb.addDoneLoadingListener(new Runnable() {
@Override
public void run() {
LayerList mainLayerList = arcGISMap.getOperationalLayers();
for (GeodatabaseFeatureTable gdbFeatureTable : localGdb.getGeodatabaseFeatureTables()) {
FeatureLayer dataFeatureLayer = new FeatureLayer(gdbFeatureTable);
mainLayerList.add(dataFeatureLayer);
// 地图服务加载监听 -- 监听地图服务是否加载成功并作出后续动作
featureLayer.addLoadStatusChangedListener(mOnStatusListener);
}
}
});
离线Geodatabase空间查询:
第一种查询方式: Identify查询
/* 使用Identify查询 -- 参数:
FeatureLayer(查询的要素图层)、
ScreenPoint(屏幕坐标点)、
误差值、
是否只返回Popup、
返回的最大结果数量 */
ListenableFuture<IdentifyLayerResult> iFeatureResult =
mMapView.identifyLayerAsync(feature, screenPoint, 5, false, 25);
// 监听异步查询 -- 当查询结束时调用
iFeatureResult.addDoneListener(new Runnable() {
@Override
public void run() {
try {
IdentifyLayerResult iResult = iFeatureResult.get();
for (GeoElement identifiedElement : iResult.getElements()) {
if (identifiedElement instanceof Feature) {
mFeature = (Feature) identifiedElement;
... do something ...
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
LogUtil.d("查询抛出异常:" + feature.getName());
}
}
});
第二种查询方式: Query查询
// 通过点击的屏幕点 -- 转为地图上的点
// 设置误差范围
int offSize = 15;
Point minPoint = new Point(mPoint.getX() - offSize, mPoint.getY() - offSize);
Point maxPoint = new Point(mPoint.getX() + offSize, mPoint.getY() + offSize);
QueryParameters queryParameters = new QueryParameters();
Envelope queryEnv = new Envelope(minPoint, maxPoint);
// 设置查询范围(查询条件)
queryParameters.setGeometry(queryEnv);
queryParameters.setReturnGeometry(true);
queryParameters.setSpatialRelationship(QueryParameters.SpatialRelationship.INTERSECTS);
// 开始异步查询
ListenableFuture<FeatureQueryResult> featureQueryResult
= featureTable.queryFeaturesAsync(queryParameters);
// 设置异步查询监听
featureQueryResult.addDoneListener(new Runnable() {
@Override
public void run() {
try {
FeatureQueryResult result = featureQueryResult.get();
Iterator<Feature> iterator = result.iterator();
Feature mFeature;
while (iterator.hasNext()) {
mFeature = iterator.next();
... do something ...
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
LogUtil.d("查询抛出异常:" + feature.getName());
}
}
});
一般情况是使用第二种方法查询,在实际使用过程中,因为遇到一些不太清楚的闪退,所以换成了第一种方法查询。