本文的代码是来自Sample里面的QueryTask的例子。自己看Api很久后,觉得还是拿这个例子作为自己的入门学习。比较省时方便。
首先需要自己了解mapview的状态监听器的Api:
OnStatusChangedListener接口用于监听MapView或Layer状态变化的监听器,用法如下:
1. //添加状态监听器
2. mapView.setOnStatusChangedListener(new OnStatusChangedListener() {
3. public void onStatusChanged(Object source, STATUS status) {
4. if(status == STATUS.INITIALIZED){
5. }else if(status == STATUS.LAYER_LOADED){
6. }else if((status == STATUS.INITIALIZATION_FAILED)){
7. }else if((status == STATUS.LAYER_LOADING_FAILED)){
8. }
9. }
10. });
下面是做个注释和调试成功过的java代码:从上面的代码我们可以清晰看到,对于MapView的状态变化主要有四种:
1) STATUS.INITIALIZED初始化成功
2) STATUS.LAYER_LOADED图层加载成功
3) STATUS.INITIALIZATION_FAILED初始化失败
4) STATUS.LAYER_LOADING_FAILED图层加载失败
/* Copyright 2010 ESRI
*
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
*
* See the sample code usage restrictions document for further information.
*
*/
package com.esri.arcgis.android.samples.querytask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.Layer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.arcgis.android.samples.attributequery.R;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.tasks.ags.query.Query;
import com.esri.core.tasks.ags.query.QueryTask;
public class AttributeQuery extends Activity {
/** Called when the activity is first created. */
MapView mv;
GraphicsLayer gl;
Graphic graphic;
Graphic fillGraphic;
Button querybt;
String targetServerURL = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer";
boolean blQuery = true;
ProgressDialog progress;
final static int HAS_RESULTS = 1;
final static int NO_RESULT = 2;
final static int CLEAR_RESULT = 3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mv = (MapView) findViewById(R.id.map);
mv.setOnStatusChangedListener(new OnStatusChangedListener() {
private static final long serialVersionUID = 1L;
public void onStatusChanged(Object source, STATUS status) {
if (source == mv && status == STATUS.INITIALIZED) {
gl = new GraphicsLayer();
SimpleRenderer sr = new SimpleRenderer(
new SimpleFillSymbol(Color.RED));// 设置渲染器
gl.setRenderer(sr);
mv.addLayer(gl);
boolean doQuery = false;
for (Layer lv : mv.getLayers()) {
if (lv instanceof ArcGISTiledMapServiceLayer) {
ArcGISTiledMapServiceLayer tLayer = (ArcGISTiledMapServiceLayer) lv;
if (tLayer.getUrl().equals(targetServerURL)) {
doQuery = true;
break;
}
}
}
if (!doQuery) {
Toast toast = Toast.makeText(AttributeQuery.this,
"URL for query does not exist any more",
Toast.LENGTH_LONG);
toast.show();
} else {
querybt.setEnabled(true);
}
}
}
});
querybt = (Button) findViewById(R.id.queryButton);
querybt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (blQuery) {
String targetLayer = targetServerURL.concat("/3");
String[] queryParams = { targetLayer, "AVGHHSZ_CY>3.5" };
AsyncQueryTask ayncQuery = new AsyncQueryTask();
ayncQuery.execute(queryParams);
} else {
gl.removeAll();
blQuery = true;
querybt.setText("Average Household > 3.5");
}
}
});
}
/**
*
* 异步执行查询任务.
*
*/
private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> {
protected void onPreExecute() {
// 在未查询出结果时显示一个进度条
progress = ProgressDialog.show(AttributeQuery.this, "",
"Please wait....query task is executing");
}
protected FeatureSet doInBackground(String... queryParams) {
if (queryParams == null || queryParams.length <= 1)
return null;
// 查询条件和URL参数
String url = queryParams[0];
// 查询所需的参数类
Query query = new Query();
String whereClause = queryParams[1];
SpatialReference sr = SpatialReference.create(102100);// 建立一个空间参考
// WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE(102100)
query.setGeometry(new Envelope(-20147112.9593773, 557305.257274575,
-6569564.7196889, 11753184.6153385));// 设置查询空间范围
query.setOutSpatialReference(sr);// 设置查询输出的坐标系
query.setReturnGeometry(true);// 是否返回空间信息
query.setWhere(whereClause);// where条件
QueryTask qTask = new QueryTask(url);// 查询任务类
FeatureSet fs = null;
Log.i(null, "doInBackground is running !");
try {
fs = qTask.execute(query);// 执行查询,返回查询结果
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return fs;
}
return fs;
}
protected void onPostExecute(FeatureSet result) {
Log.i(null, "onPostExecute is running !");
String message = "No result comes back";
if (result != null) {
Graphic[] grs = result.getGraphics();
if (grs.length > 0) {
// gl = new GraphicsLayer();
// SimpleRenderer sr = new SimpleRenderer(
// new SimpleFillSymbol(Color.RED));// 设置渲染器
// gl.setRenderer(sr);
gl.addGraphics(grs); // 将查询结果添加到图层上
// mv.addLayer(gl);
message = (grs.length == 1 ? "1 result has " : Integer
.toString(grs.length) + " results have ")
+ "come back";
}
}
progress.dismiss();// 停止进度条
Toast toast = Toast.makeText(AttributeQuery.this, message,
Toast.LENGTH_LONG);
toast.show();
querybt.setText("Clear graphics");
blQuery = false;
Log.i(null, "onPostExecute is finish !");
}
}
@Override
protected void onPause() {
super.onPause();
mv.pause();
}
@Override
protected void onResume() {
super.onResume();
mv.unpause();
}
}
从中,可以看出重点在于对query和query Task的定义上,url必须要知道你是需要查询的内容是什么方面的。其中可以借鉴的就是那个进度条的使用,该什么时候显示出来,什么时候关闭它。可以用于以后大量的搜索的功能上做一个过渡。
效果图: