布局文件为:
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".HighlightFeatureActivity" >
<com.esri.android.map.MapView
android:id="@+id/mmapview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.esri.android.map.MapView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="河流" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="37dp"
android:enabled="false"
android:text="公路" />
</LinearLayout>
</RelativeLayout>
当点击button1,然后在MapView中的河流处长按就选中这个河流,并高亮显示了,如果点击button2,然后在MapView中的河公路处长按就选中这条公路,并高亮显示了。
public class HighlightFeatureActivityextends Activity implements
OnClickListener{
MapViewmMapView;
privateGraphicsLayer glayer;
privateGraphic[] graphics;
privateButton btn_water;
privateButton btn_highWay;
privateint selectId;//保存要查询图层的id
/**Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView= (MapView) findViewById(R.id.mmapview);
btn_water= (Button) findViewById(R.id.button1);
btn_highWay= (Button) findViewById(R.id.button2);
btn_highWay.setOnClickListener(this);
btn_water.setOnClickListener(this);
mMapView.setOnStatusChangedListener(newOnStatusChangedListener() {
privatestatic final long serialVersionUID = 1L;
publicvoid onStatusChanged(Object arg0, STATUS arg1) {
if(arg0 == mMapView && arg1 == STATUS.INITIALIZED) {
btn_highWay.setEnabled(true);
btn_water.setEnabled(true);
}
}
});
mMapView.setOnLongPressListener(newOnLongPressListener() {
privatestatic final long serialVersionUID = 1L;
publicvoid onLongPress(float arg0, float arg1) {
if(selectId > 0) {
Pointclickpoint = mMapView.toMapPoint(arg0, arg1);//
IdentifyParameters inputParameters = newIdentifyParameters();
//设置查询范围为长按处。
inputParameters.setGeometry(clickpoint);
inputParameters.setLayers(new int[]{ selectId });//设置查询的图层
Envelope env = new Envelope();
mMapView.getExtent().queryEnvelope(env);
inputParameters.setMapExtent(env);
inputParameters.setSpatialReference(mMapView.getSpatialReference());
inputParameters.setDPI(96);
inputParameters.setMapHeight(mMapView.getHeight());
inputParameters.setMapWidth(mMapView.getWidth());
inputParameters.setTolerance(10);//设置容限
myIdentifyTask mIdenitfy = newmyIdentifyTask();
mIdenitfy.execute(inputParameters);
}
}
});
ArcGISTiledMapServiceLayertile = new ArcGISTiledMapServiceLayer(
"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/PublicSafety/PublicSafetyBasemap/MapServer");
glayer= new GraphicsLayer();
mMapView.addLayer(tile);
mMapView.addLayer(glayer);
}
publicvoid onClick(View v) {
switch(v.getId()) {
caseR.id.button1:
selectId= 28;
break;
caseR.id.button2:
selectId= 13;
break;
}
}
@Override
protectedvoid onDestroy() {
super.onDestroy();
}
@Override
protectedvoid onPause() {
super.onPause();
mMapView.pause();
}
@Override
protectedvoid onResume() {
super.onResume();
mMapView.unpause();
}
/**
* 异步查询类
*/
publicclass myIdentifyTask extendsAsyncTask<IdentifyParameters, Void, IdentifyResult[]>{
privateIdentifyTask task;
@Override
protectedIdentifyResult[] doInBackground(IdentifyParameters... params) {
try{
IdentifyResult[]res= task.execute(params[0]);
returnres;
}catch (Exception e) {
e.printStackTrace();
returnnull;
}
}
@Override
protectedvoid onPreExecute() {
task=newIdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/PublicSafety/PublicSafetyBasemap/MapServer");
}
@Override
protectedvoid onPostExecute(IdentifyResult[] result) {
if(result.length<=0){
Toast.makeText(HighlightFeatureActivity.this,"没找到", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(HighlightFeatureActivity.this,"找到"+result.length, Toast.LENGTH_SHORT).show();
}
graphics=new Graphic[result.length];
//取得查询得到的Graphic数组,并进遍历判断每个Graphic的类型,制作加到glayer中,展现出来
for(inti = 0; i < result.length; i++){
Geometryg= result[i].getGeometry();
com.esri.core.geometry.Geometry.Typetype= g.getType();
if(type==Geometry.Type.POLYLINE){
SimpleLineSymbolline=new SimpleLineSymbol(Color.RED, 10);
graphics[i]=newGraphic(g, line);
}elseif(type==Geometry.Type.POLYGON){
SimpleFillSymbol fillsym=new SimpleFillSymbol(Color.RED);
graphics[i]=newGraphic(g, fillsym);
}
}
glayer.addGraphics(graphics);
}
}
}
选中河流的结果: