Arcgis For Android 有三种常用的查询方式:IdentifyTask 、FindTask 和 QueryTask。
1、QueryTask
QueryTask是一个进行空间和属性查询的功能类,它可以在某个地图服务的某个子图层内进行查询,QueryTask进行查询的地图服务并不必须加载到Map中进行显示。QueryTask的执行需要两个先决条件:一个是需要查询的图层URL(需要到具体的子图层)、一个是进行查询的过滤条件。
这里创建一个AsyncQueryTask 用于查询:
/** * 执行 "根据搜索条件" 查询的任务 * @author Administrator */ public class AsyncQueryTask extends AsyncTask<String, Void, FeatureResult> { private ProgressDialog progressDialog; //查询提示dialog UserCredentials user_sxq; // Arcgis 用户凭证 private Context context; public AsyncQueryTask(Context context, ProgressDialog progressDialog, UserCredentials user_sxq) { this.context = context; this.progressDialog = progressDialog; this.user_sxq = user_sxq; } @Override protected void onPreExecute() { // 查询前执行的方法 progressDialog.show(); // 搜索中,请耐心等待 } @SuppressWarnings("null") @Override protected FeatureResult doInBackground(String... params) { // 查询中 FeatureResult fs = null; if(params == null && params.length <= 1){ return null; } try { String whereClause = params[1]; QueryParameters queryParameters = new QueryParameters(); queryParameters.setReturnGeometry(true); queryParameters.setWhere(whereClause); queryParameters.setOutFields(new String[] { "*" }); SpatialReference sr; // 查询的坐标系 -- 图层服务使用的坐标系 if(Constants.Map_Gis_Type == 0){ sr = SpatialReference.create(Constants.Map_Gis_Type_Str); }else{ sr = SpatialReference.create(Constants.Map_Gis_Type); } queryParameters.setOutSpatialReference(sr); QueryTask queryTask = new QueryTask(params[0], user_sxq); fs = queryTask.execute(queryParameters); } catch (Exception e) { e.printStackTrace(); } return fs; } @Override protected void onPostExecute(FeatureResult result) { // 查询结束后执行的方法 //doSomeThing } }
在代码中调用查询方法
/** * 根据条件搜索, 供给RightMenuFragment使用 * @param condition 查询过滤条件 * @param queryUrl 待查询的图层url */ public void searchByCondition(String condition, String queryUrl) { if (!mMapView.isLoaded()) return; if (!(condition.length() > 0)) return; AsyncQueryTask asyncQueryTask = new AsyncQueryTask(getActivity(), progressDialog, user_sxq); LogUtil.d("queryUrl" + queryUrl); String[] queryParams = {queryUrl, condition}; asyncQueryTask.execute(queryParams); }
2、FindTask
FindTask允许对地图中一个或多个图层的要素进行基于属性字段值的查询(search one or more layers in a map for features with attribute values that match or contain an input value)。FindTask不能进行“空间查询”,因为FindTask可以对多个图层进行查询,所有它的url属性需要指向所查询的地图服务的URL,而不像QueryTask需要指定某个图层的URL。FindTask查询 是先通过关键字匹配待查字段查询出结果,如果有过滤条件,再从结果总过滤出符合条件的结果。
在代码中使用:
List<FindResult> res; /** * 根据条件搜索, 供给RightMenuFragment使用,多图层一起查询 * @param condition // 查询条件 * @param searchKey// 查询关键字 (设置查询参数的时候必须设置) * @param fields // 待查询的字段名称 数组 * @param layerIds // 查询服务的子图层的id数组 * @param findLayer // 待查询的服务URL */ public void searchFromLayers(String condition ,String searchKey,String[] fields , int[] layerIds, String findLayer { if (progressDialog != null && !progressDialog.isShowing()){ progressDialog.show(); } if (!mMapView.isLoaded()) return; if (!(condition.length() > 0)) return; final FindTask findTask = new FindTask(findLayer); final FindParameters findParameters = new FindParameters(); findParameters.setLayerIds(layerIds); if (fields.length == 1){ findParameters.setReturnGeometry(true); //允许返回几何图形 findParameters.setSearchText(searchKey); // 设置查询关键字--必须设置 findParameters.setSearchFields(fields); // 设置查询字段的名称 }else { Map<Integer,String> parmMap = new HashMap<>(); for (int a : layerIds){ parmMap.put(a,condition); // 设置每个子图层的 查询过滤条件 } findParameters.setLayerDefs(parmMap); findParameters.setReturnGeometry(true); findParameters.setSearchText(searchKey); findParameters.setSearchFields(fields); } new Thread(new Runnable() { @Override public void run() { try { res = findTask.execute(findParameters); // res就是查询到的结果 List<FindResult> } catch (Exception e) { e.printStackTrace(); } } }).start(); }
3、IdentifyTask
IdentifyTask是一个在地图服务中识别要素(Feature)的功能类。通过IdentifyTask可以搜索地图层中与输入几何形相交的要素(search the layers in a map for features that intersect an input geometry)。因为也是在多个图层中查询,所以Task的URL是动态图层服务的地址。同样,返回的要素都可以作为Graphic被添加到地图的GraphicsLayer上。
//实例化对象,并且给实现初始化相应的值 params = new IdentifyParameters(); params.setTolerance(20); params.setDPI(98); params.setLayers(new int[]{4}); params.setLayerMode(IdentifyParameters.ALL_LAYERS); //从map的一个点击地图的事件监听 获取点击事件并设置查询参数 public void onSingleTap(final float x,final float y) { if(!map.isLoaded()){ return; } //establish the identify parameters // 点击的点 Point identifyPoint = map.toMapPoint(x, y); params.setGeometry(identifyPoint); params.setSpatialReference(map.getSpatialReference()); // 设置坐标系 params.setMapHeight(map.getHeight()); params.setMapWidth(map.getWidth()); Envelope env = new Envelope(); map.getExtent().queryEnvelope(env); params.setMapExtent(env); //我们自己扩展的异步类 MyIdentifyTask mTask = new MyIdentifyTask(identifyPoint); mTask.execute(params);//执行异步操作并传递所需的参数 } });
自建的IdentifyTask
public class MIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]> { Point point; String layerIp; // 查询的图层(具体到子图层) IdentifyTask identifyTask; public MIdentifyTask(Point point, String layerIp) { this.point = point; this.layerIp = layerIp; } @Override protected void onPreExecute() { // 查询前执行 identifyTask = new IdentifyTask(layerIp); } @Override protected IdentifyResult[] doInBackground(IdentifyParameters... params) { IdentifyResult[] mResult = null; if (params != null && params.length > 0) { try { mResult = identifyTask.execute(params[0]); } catch (Exception e) { e.printStackTrace(); } } return mResult; } @Override protected void onPostExecute(IdentifyResult[] result) { //查询完成后执行 if (result == null) { IdentifyResult[] r = {}; EventBus.getDefault().post(r); } else { EventBus.getDefault().post(result); } } }
三种查询的返回结果:
QueryTask:返回的是一个FeatureSet。Featureset.features[i]可以加入到GraphicsLayer上显示,也可以通过Attributes属性字段得到属性信息。
FindTask:返回的是一个FindResults数组, FindResults[i].feature可以加入到GraphicsLayer上显示,也可以通过Attributes属性字段得到属性信息。
IdentifyTask:返回的是一个IdentifyResults数组,IdentifyResults[i].feature可以加入到GraphicsLayer上显示,也可以通过Attributes属性字段得到属性信息。
作者:曾经的追风少年
链接:http://www.jianshu.com/p/86a4159df60b
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Arcgis For Android 查询功能
最新推荐文章于 2024-08-01 17:18:36 发布