本人第一次试用IdentiFyTask做查询工作,
1.试用了天地图图层,这个是网上下载到的,
2.导入ArcGis for android 的相关jia包
主要代码如下:::
(1)使用AsyncTask进行IdentifyTask 的查询
public class MyIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]>{
IdentifyTask identifyTask;
private ProgressDialog dialog;
Context context;
MapView map;
Point point;
public MyIdentifyTask(Point point, Context context,MapView map){
this.context = context;
this.map = map;
this.point = point;
}
@Override
protected IdentifyResult[] doInBackground(IdentifyParameters... params) {
IdentifyResult[] mResult = null;
if(params!=null&¶ms.length>0){
try {
mResult = identifyTask.execute(params[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
return mResult;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "Identify Task", "Identify query ...");
identifyTask = new IdentifyTask("http://221.13.129.100:6080/arcgis/rest/services/ZZWebGisData/MapServer");
}
@Override
protected void onPostExecute(IdentifyResult[] result) {
if(dialog.isShowing()){
dialog.dismiss();
Callout out = map.getCallout();
TextView view = new TextView(context);
if(result.length>0){
view.setText(result[0].getLayerName());
}else{
view.setText("暂无信息");
}
out.setContent(view);
out.show(point);
}
}
}
(2) MainActivity如下
public class MainActivity extends Activity {
private MapView map;
private com.example.identifytask.TianDiTuTiledMapServiceLayer tianDiTuLayer;
private com.example.identifytask.TianDiTuTiledMapServiceLayer tianDiTuLayer2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (MapView)findViewById(R.id.map);
//添加天地图作为底图图层(使用的上面下载到图层转换代码)
tianDiTuLayer = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.VEC_C);
//添加天地图的标注图层
tianDiTuLayer2 = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.CVA_C);
//添加自己的图层
ArcGISDynamicMapServiceLayer dLayer = new ArcGISDynamicMapServiceLayer("http://221.13.129.100:6080/arcgis/rest/services/ZZWebGisData/MapServer");
//设置地图背景色(不添加地图的时候能够看出来,白色无网格)
map.setMapBackground(0xffffff, 0xffffff, 0, 0);
//去除前面的use only的水印
ArcGISRuntime.setClientId("1eFHW78avlnRUPHm");
map.addLayer(tianDiTuLayer);
map.addLayer(tianDiTuLayer2);
map.addLayer(dLayer);
map.setOnSingleTapListener(new OnSingleTapListener() {
@Override
public void onSingleTap(float x, float y) {
if(!map.isLoaded()){
return;
}
//坐标转换
Point identifyPoint = map.toMapPoint(x, y);
Point point = (Point)GeometryEngine.project(identifyPoint, SpatialReference.create(4490), SpatialReference.create(2383));
IdentifyParameters params = new IdentifyParameters();
params.setGeometry(point);
params.setSpatialReference(SpatialReference.create(2383));
params.setDPI(98);
params.setTolerance(20);
params.setMapHeight(map.getHeight());
params.setMapWidth(map.getWidth());
params.setLayerMode(IdentifyParameters.ALL_LAYERS);
Envelope env = new Envelope();
map.getExtent().queryEnvelope(env);
params.setMapExtent(env);
//实例化自己定义的类
MyIdentifyTask myIdentifyTask = new MyIdentifyTask(identifyPoint,MainActivity.this,map);
myIdentifyTask.execute(params);
}
});
}
}