arcgis for android QueryTask的簡單使用

來源:互聯網
上載者:User

布局檔案:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    tools:context=".QueryFeatureActivity"

    >

<Button android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:id="@+id/select"

   android:text="尋找"

   android:onClick="onSelect"/>

<com.esri.android.map.MapView

    android:layout_weight="1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/mmapView"

    >   

</com.esri.android.map.MapView>

</LinearLayout>

 

點擊Button  之後會查詢MapView當前範圍中符合查詢條件的圖斑。

 

public
class
QueryFeatureActivity extends Activity {

    MapView mMapView ;

    private GraphicsLayerglayer;

    private Graphic[]graphics;

    private StringmapUrl;

    /** Calledwhen the activity is first created. */

    @Override

    public
void
onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       mMapView = (MapView) findViewById(R.id.mmapView);

     mapUrl="http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer";

       ArcGISTiledMapServiceLayer tile=new ArcGISTiledMapServiceLayer(mapUrl);

       glayer=new GraphicsLayer();

       SimpleRendererred=new SimpleRenderer(new SimpleFillSymbol(Color.RED));

       glayer.setRenderer(red);

       mMapView.addLayer(tile);

       mMapView.addLayer(glayer);

    }

   

    //查詢按鈕點擊事件

    public
void
onSelect(View v){

    Queryquery=new Query();

    Envelopeen=new Envelope();

    mMapView.getExtent().queryEnvelope(en);

    query.setGeometry(en);//查詢範圍

    query.setOutSpatialReference(mMapView.getSpatialReference());//設定輸入的空間參考

    query.setInSpatialReference(mMapView.getSpatialReference());//設定輸出的空間參考

    query.setReturnGeometry(true);//返回查詢

    query.setWhere("AVGHHSZ_CY>1.5");//查詢條件

    MyQueryTasktask=new MyQueryTask();

    task.execute(query);  

    }

   

    //查詢非同步類

    class MyQueryTaskextends AsyncTask<Query, Void,FeatureSet>{

    private ProgressDialogdia;

    private QueryTasktask;

       protected FeatureSet doInBackground(Query... params) {

           Query query=params[0];

           FeatureSet res=null;

           try {

              res= task.execute(query);    

           } catch (Exception e) {

              e.printStackTrace();

           }

           return res;

       }

       protected
void
onPreExecute() {

           dia=new ProgressDialog(QueryFeatureActivity.this);

           dia.setMessage("正在查詢。。。");

           dia.show();

           task=new QueryTask(mapUrl.concat("/3"));//設定查詢圖層

       }

       protected
void
onPostExecute(FeatureSet result) {

           String message="沒有找到。。。";

           if(result!=null){

              graphics=result.getGraphics();

              if(graphics.length>0){//有資料返回

                  glayer.addGraphics(graphics);//把graphics[]添加到GraphicLayer中顯示出來

                  message="查詢到"+graphics.length +"結果";

              }

           }

           Toast.makeText(QueryFeatureActivity.this, message, Toast.LENGTH_SHORT).show();

           dia.dismiss();

       }

    }

   

    protected
void
onDestroy() {

       super.onDestroy();

 }

    @Override

    protected
void
onPause() {

       super.onPause();

       mMapView.pause();

 }

    @Override protectedvoid onResume() {

       super.onResume();

       mMapView.unpause();

    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.