arcgis for android 学习 - (6) 属性查询。检索图层上某个要素,并高亮显示它。

"NAME like '%" + keyQuery + "%'" 既然是 “地图”,“gis”,那么 查询(QUERY)是必须的了。我们看看如何查询。

 

动机:

我想要查找地图上的某个点(要素,或者说某个位置 ),然后将检索的结果 显示出来(高亮)显示。

 

 执行查询:

// 查询的关键字
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";
}
 
}

 

你是否有了疑问:

为什么 我要写这么一句 

 

// 指定查询的目标图层
String queryUrl = URL_LAYER + "/0"; 

 

因为:你的图层服务URL地址可能包含有多个图层,而rest格式的url用  “/0 ” 这样的“方式”表示第0个图层。那么我们怎么知道要写0,或者1,2,3等什么呢?这要看你的地图制作者为你提供的内容了。我们再浏览器里打开我们的服务地址可以看到下面这样:

 

 

 

注意到上面的 Layers:  省(0)了么? 这里的 0 ,就是第0个图层。

 

为什么查询的约束条件要这么写?

// 类似sql语句的查询 where语句
query.setWhere("NAME like '%" + keyQuery + "%'");

 

 这其实是这么的一个查询方式 ,比如: NAME like '%北京%' 。 是不是很像sql语句里的like语句啊。它的意思是查询 “字段属性名称是NAME”的”值“,这里”是NAME里包含了 北京 这两个字的 对象“,这里的对象,就是你查询的结果,也就是返回的Graphic,这个对象,和字段(属性)是地图制作者向你提供的。我在浏览器打开我的地图服务,可以看到下马这样:

 

注意 Filelds:  下面的描述了么? "NAME (Type:esriFieldTypeString,Alias:NAME). 它描述的内容是:”字符串格式,字段名称是NAME“ 

 

----- -------------------

 

 代码如下

布局:

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    
>

    <TextView
        
android:layout_width="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="请输入查询关键字:" />

    <EditText
        
android:id="@+id/txtQueryString"
        android:layout_width
="match_parent"
        android:layout_height
="wrap_content"
        android:ems
="10" android:text="北京" >
 
        <requestFocus />
    </EditText>
<Button
    
android:id="@+id/queryButton"  
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"
    android:layout_gravity
="center"
    android:text
="执行查询"
    
/>   
    
<com.esri.android.map.MapView
    
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
="@+id/map" android:layout_width="fill_parent"
    android:layout_height
="fill_parent" url="http://www.arcgis.com/home/webmap/viewer.html?webmap=58e58acce685442aa4c73f91fbb3a387"
    
> 
</com.esri.android.map.MapView>
</LinearLayout>

代码:

 package demo;


import java.util.Random;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.core.geometry.Envelope;
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.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;
import com.esri.core.tasks.ags.query.Query;
import com.esri.core.tasks.ags.query.QueryTask;

import demo.attrbuteQuery.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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.09751954817561.93785559,
                12996377.826274884902.95977474);
        mMapView.setExtent(initextext);
    }

    /*
     * 获得 GetGraphicLayer
     
*/
    private GraphicsLayer GetGraphicLayer() {
        if (mGraphicsLayer == null) {
            mGraphicsLayer = new GraphicsLayer();
            mMapView.addLayer(mGraphicsLayer);
        }
        return mGraphicsLayer;
    }
}

 

posted on 2012-06-06 10:56  张云飞VIR  阅读(4922)  评论(10编辑  收藏  举报