arcgis for Android 高亮显示选中要素

布局文件为:

<?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);

                   }

                  

         }

 

}

 

 

选中河流的结果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值