public class attrbuteQueryActivity extends Activity {
Button mQueryButton;
MapView mMapView;
final String URL_LAYER = "http://192.168.3.130/ArcGIS/rest/services/China/MapServer";
GraphicsLayer mGraphicsLayer;
EditText txtQueryString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtQueryString = (EditText) findViewById(R.id.txtQueryString);
mQueryButton = (Button) findViewById(R.id.queryButton);
mQueryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 查询的关键字
String keyQuery = txtQueryString.getText().toString();
Query query = new Query();
// 类似sql语句的查询 where语句
query.setWhere("NAME like '%" + keyQuery + "%'");
query.setReturnGeometry(true);
// 指定查询的目标图层
String queryUrl = URL_LAYER + "/0";
// 构建查询任务对象QueryTask
QueryTask queryTask = new QueryTask(queryUrl);
FeatureSet fs = null;//结果集
try {
fs = queryTask.execute(query);//执行查询
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GraphicsLayer graphicsLayer = GetGraphicLayer();
String message = "No result comes back";
if (fs != null && graphicsLayer.isInitialized() && graphicsLayer.isVisible()) {
//获得结果集内的 graphics 对象
Graphic[] grs = fs.getGraphics();
if (grs.length > 0) {
//构建 简单填充符号,该对象指定了一种“呈现方式”
SimpleFillSymbol symbol = new SimpleFillSymbol(
Color.RED);
//设定呈现方式
graphicsLayer.setRenderer(new SimpleRenderer(symbol));
//添加 graphic带图层,这时,会自动用刚刚指定的“呈现方式”来呈现
graphicsLayer.removeAll();//移除以前的
graphicsLayer.addGraphics(grs);
message = (grs.length == 1 ? "1 result has " : Integer
.toString(grs.length) + " results have ")
+ "come back";
}
}
Toast toast = Toast.makeText(attrbuteQueryActivity.this,
message, Toast.LENGTH_LONG);
toast.show();
}
});
mMapView = (MapView) findViewById(R.id.map);
// Add layer to MapView
mMapView.addLayer(new com.esri.android.map.ags.ArcGISDynamicMapServiceLayer(
URL_LAYER));
Envelope initextext = new Envelope(12891659.0975195, 4817561.93785559,
12996377.82627, 4884902.95977474);
mMapView.setExtent(initextext);
}
/*
* 获得 GetGraphicLayer
*/
private GraphicsLayer GetGraphicLayer() {
if (mGraphicsLayer == null) {
mGraphicsLayer = new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
}
return mGraphicsLayer;
}
Button mQueryButton;
MapView mMapView;
final String URL_LAYER = "http://192.168.3.130/ArcGIS/rest/services/China/MapServer";
GraphicsLayer mGraphicsLayer;
EditText txtQueryString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtQueryString = (EditText) findViewById(R.id.txtQueryString);
mQueryButton = (Button) findViewById(R.id.queryButton);
mQueryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 查询的关键字
String keyQuery = txtQueryString.getText().toString();
Query query = new Query();
// 类似sql语句的查询 where语句
query.setWhere("NAME like '%" + keyQuery + "%'");
query.setReturnGeometry(true);
// 指定查询的目标图层
String queryUrl = URL_LAYER + "/0";
// 构建查询任务对象QueryTask
QueryTask queryTask = new QueryTask(queryUrl);
FeatureSet fs = null;//结果集
try {
fs = queryTask.execute(query);//执行查询
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GraphicsLayer graphicsLayer = GetGraphicLayer();
String message = "No result comes back";
if (fs != null && graphicsLayer.isInitialized() && graphicsLayer.isVisible()) {
//获得结果集内的 graphics 对象
Graphic[] grs = fs.getGraphics();
if (grs.length > 0) {
//构建 简单填充符号,该对象指定了一种“呈现方式”
SimpleFillSymbol symbol = new SimpleFillSymbol(
Color.RED);
//设定呈现方式
graphicsLayer.setRenderer(new SimpleRenderer(symbol));
//添加 graphic带图层,这时,会自动用刚刚指定的“呈现方式”来呈现
graphicsLayer.removeAll();//移除以前的
graphicsLayer.addGraphics(grs);
message = (grs.length == 1 ? "1 result has " : Integer
.toString(grs.length) + " results have ")
+ "come back";
}
}
Toast toast = Toast.makeText(attrbuteQueryActivity.this,
message, Toast.LENGTH_LONG);
toast.show();
}
});
mMapView = (MapView) findViewById(R.id.map);
// Add layer to MapView
mMapView.addLayer(new com.esri.android.map.ags.ArcGISDynamicMapServiceLayer(
URL_LAYER));
Envelope initextext = new Envelope(12891659.0975195, 4817561.93785559,
12996377.82627, 4884902.95977474);
mMapView.setExtent(initextext);
}
/*
* 获得 GetGraphicLayer
*/
private GraphicsLayer GetGraphicLayer() {
if (mGraphicsLayer == null) {
mGraphicsLayer = new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
}
return mGraphicsLayer;
}
}