ArcGIS for Android FeatureLayer的属性更新

参考api中所给的属性编辑的例子,写了一个非常简单的属性编辑的程序,单击地图中FeatureLayer的图斑,可以实现选中图斑,并在log中打印出该图斑的所有属性信息,之后点击修改按钮,可以将选中图斑中字段为field_name的值改为“哈哈”。(更新其他属性也可原理类似,可以参考api中提供的AttributeEditor的例子)

代码如下:

public class AttributeEditTestActivity extends Activity {
	
	MapView mMapView ;
	 ArcGISFeatureLayer afl;
	 Map<String,Object> atts;
	 String obID;
	 ArcGISDynamicMapServiceLayer dmsl ;
	 boolean hasSelected=false;
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		mMapView = (MapView) findViewById(R.id.mapV);
		Envelope initextent = new Envelope(-10868502.895856911, 4470034.144641369,
				-10837928.084542884, 4492965.25312689);
		mMapView.setExtent(initextent, 0);
		ArcGISTiledMapServiceLayer tmsl = new ArcGISTiledMapServiceLayer(
				"http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
		mMapView.addLayer(tmsl);

		 dmsl = new ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSFields/MapServer");
		mMapView.addLayer(dmsl);
		afl=new ArcGISFeatureLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSFields/FeatureServer/0",
				ArcGISFeatureLayer.MODE.SELECTION);
		mMapView.addLayer(afl);
		mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
			
			public void onStatusChanged(Object arg0, STATUS arg1) {
				if(arg0==mMapView && arg1==STATUS.INITIALIZED)
				mMapView.setOnSingleTapListener(new mySingleTagLis());
				//为ArcGISFeatureLayer设置被选中的颜色
				SimpleFillSymbol selectSym=new SimpleFillSymbol( Color.BLUE);
				selectSym.setAlpha(50);
				selectSym.setOutline(new SimpleLineSymbol(Color.RED, 3));
				afl.setSelectionSymbol(selectSym);
			}
		});
    }
    
    
    /**
     * 单击地图的监听
     */
    class mySingleTagLis implements OnSingleTapListener{
		public void onSingleTap(float arg0, float arg1) {
			Point p=mMapView.toMapPoint(arg0 ,arg1);
			Query q=new Query();
			//设置Query的各个参数
			q.setInSpatialReference(mMapView.getSpatialReference());
			q.setReturnGeometry(true);
			q.setSpatialRelationship(SpatialRelationship.INTERSECTS);
			q.setGeometry(p);
			//执行选择
			afl.selectFeatures(q, SELECTION_METHOD.NEW, new qCallBackLis());
		}
    }
    
    /**
     * 
     *执行选择ArcGISFeatureLayer的回调
     */
    class qCallBackLis implements CallbackListener<FeatureSet>{

		public void onCallback(FeatureSet fet) {
	     if(fet.getGraphics().length>0){
	    	 hasSelected=true;
	    	Graphic g=fet.getGraphics()[0];//得到选择的Graphic
	    	obID= g.getAttributeValue(afl.getObjectIdField()).toString();//保存ObjectID的值
	    	 atts= g.getAttributes();
	    	 //得到包含所有属性的Map集合,并进行遍历之后将属性和属性值打印在Log的上
	    	 Set<Entry<String ,Object>> ents= atts.entrySet();
	    	for(Entry<String ,Object> ent:ents){
	    		Log.i("Attribute ",ent.getKey()+"  :  "+ent.getValue());
	    	}	    	
	     }			
		}
		public void onError(Throwable arg0) {
			Log.i("AttributeEditTestActivity", "qCallBackLis 出错啦"+arg0.getMessage());			
		}   	
    }
    
    public void btn_click(View v){
    	if(!hasSelected){
    		return;
    	}
    	Toast.makeText(getApplicationContext(), "开始提交", 0).show();
    	atts.put("field_name", "哈哈");
    	
    	//atts.put(afl.getObjectIdField(), obID);//这里可以不用给objectID赋值,因为atts中已经有该值了,
    	                                         //但在其他情况下一定要记得给objectID赋值,这样server才知道更新那个Graphic的属性
    	Graphic g=new Graphic(null, null, atts, null);
    	afl.applyEdits(null, null, new Graphic[]{g}, new myEditLis());
    }
    
    //执行更新操作的回调
    class myEditLis implements CallbackListener<FeatureEditResult[][]>{
		public void onCallback(FeatureEditResult[][] fets) {
			if(fets[2]!=null && fets[2][0]!=null && fets[2][0].isSuccess()){
				Log.i("AttributeEditTestActivity", "edit 成功啦");
				runOnUiThread( new Runnable() {//需要在主线程中才能根系UI
					public void run() {
					     dmsl.refresh();
						Toast.makeText(getApplicationContext(), "edit 成功啦", 0).show();
					}
				});
			}
			
		}

		public void onError(final Throwable arg0) {
			Log.i("AttributeEditTestActivity", "edit 出错啦"+arg0.getMessage());
			runOnUiThread( new Runnable() {
				public void run() {
					Toast.makeText(getApplicationContext(), "edit 出错啦"+arg0.getMessage(), 0).show();
				}
			});
		}
    }
    

	@Override 
	protected void onDestroy() { 
		super.onDestroy();
 }
	@Override
	protected void onPause() {
		super.onPause();
		mMapView.pause();
 }
	@Override 	protected void onResume() {
		super.onResume(); 
		mMapView.unpause();
}


打印出来的Log:


下载全部代码


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值