对于ArcGIS的查询功能,这在开发当中是必不可少的功能,所以今天就操作了一天的identify。现在对其的基本操作已经有所了解,至少当需要这个功能时,能操作出来。所以笔记是每天必不可少的的。至少对自己的学习经历负责。我选用的例子,自然是Sample中的IdentifyTask的例子。还有参考一篇非常棒的博文:
http://blog.csdn.net/arcgis_mobile/article/details/8263412 (说它棒,是因为它能让我这样的小白看懂)
首先,了解API中的介绍:(根据个人能力进行的翻译,有错误,请指正!)
IdentifyParameters是对于输入参数中所使用的实例化IdentifyTask对象
IdentifyTask的查询后的返回结果集(是个数组)
基于Parameters的查询任务IdentifyTask
IdentifyParameters的constants介绍
查询所有服务上的图层
int
查询在服务的最上面的图层
int
查询可见图层
然后,要明白
IdentifyTask是用来识别图层中的要素的,而QueryTask是用来做图层要素查询的。
下面是我将Demo里面的代码进行简化后的代码,只是实现在点击地图上的某个地方时,跳出一个空的callout提示框。
package com.esri.arcgis.android.samples.identifytask;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageButton;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.tasks.ags.identify.IdentifyParameters;
import com.esri.core.tasks.ags.identify.IdentifyResult;
import com.esri.core.tasks.ags.identify.IdentifyTask;
public class Identify extends Activity {
MapView map = null;
IdentifyParameters params;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
map = new MapView(this);
ArcGISTiledMapServiceLayer basemap = new ArcGISTiledMapServiceLayer(
"http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.addLayer(basemap);
ArcGISTiledMapServiceLayer layer = new ArcGISTiledMapServiceLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer");
map.addLayer(layer);
// Create an extent for initial extent
Envelope env = new Envelope(-19332033.11, -3516.27, -1720941.80,
11737211.28);
// Set the MapView initial extent
map.setExtent(env);
setContentView(map);
params = new IdentifyParameters();
params.setTolerance(20);
params.setDPI(98);
params.setLayers(new int[] { 4 });
params.setLayerMode(IdentifyParameters.ALL_LAYERS);
map.setOnSingleTapListener(new OnSingleTapListener() {
private static final long serialVersionUID = 1L;
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);
}
});
}
@Override
protected void onPause() {
super.onPause();
map.pause();
}
@Override
protected void onResume() {
super.onResume();
map.unpause();
}
/*
* 异步执行查询任务
*/
private class MyIdentifyTask extends
AsyncTask<IdentifyParameters, Void, IdentifyResult[]> {
IdentifyTask mIdentifyTask;
Point mAnchor;// 用于定位callout显示的位置
MyIdentifyTask(Point anchorPoint) {
mAnchor = anchorPoint;
}
@Override
protected IdentifyResult[] doInBackground(IdentifyParameters... params) {
IdentifyResult[] mResult = null;
if (params != null && params.length > 0) {
IdentifyParameters mParams = params[0];
try {
mResult = mIdentifyTask.execute(mParams);// 执行识别任务
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return mResult;
}
@Override
protected void onPostExecute(IdentifyResult[] results) {
// TODO Auto-generated method stub
ArrayList<IdentifyResult> resultList = new ArrayList<IdentifyResult>();
for (int index = 0; index < results.length; index++) {
if (results[index].getAttributes().get(
results[index].getDisplayFieldName()) != null)
resultList.add(results[index]);
}
Context context = null;
ImageButton btn;
btn = new ImageButton(context);
btn.setBackgroundResource(R.drawable.mm4);
map.getCallout().show(mAnchor, btn);
}
@Override
protected void onPreExecute() {
mIdentifyTask = new IdentifyTask(
"http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer");
}
}
}
实现的效果:
大家根据自己的需求去给地图添加不同的功能,和给callout内部增加不同的内容。
从代码中,可以了解到,创建一个identifytask查询:
1.创建需要识别的参数对象identifyParameters;
2.设置识别的条件;
3.
定义M
yIdentifyTask
类并继承AsyncTask;
4.在MyIdentifyTask的doInBackground()方法中执IdentifyTask的execute()。