FeatureLayer是要素圖層,也是Arcgis的主要圖層。用這個圖層可以載入本地的shp檔案。下面我們看怎樣載入shp檔案到MapView中。檢視ArcGis API可知FeatureLayer的構造方法要傳一個FeatureTable,而FeatureTable可用子類ShapefileFeatureTable來實現。程式碼如下,其中shpPath為shp檔案的本地路徑。\
- private void initLayer() {
- try {
- featureLayer = new FeatureLayer(new ShapefileFeatureTable(
- shpPath));
- featureLayer.setRenderer(new SimpleRenderer(new SimpleFillSymbol(
- Color.GREEN)));
- mapView.addLayer(featureLayer);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
效果圖如下。
這樣就可以很簡單地載入shp檔案了。下面我們來學習一下FeatureLayer的查詢。
一、FeatureLayer點選查詢主要用到的方法是featureLayer.getFeatureIDs(float x, float y, int tolerance),方法與上一講GraphicsLayer的查詢差不多。可以獲取每個要素的屬性屬性值。
- private void handleSingleTap(float x, float y) {
- long[] featureIds = featureLayer.getFeatureIDs(x, y, 8);
- if (featureIds != null && featureIds.length > 0) {
- for (long id : featureIds) {
- com.esri.core.map.Feature feature = featureLayer.getFeature(id);
- Map<String, Object> attrs = feature.getAttributes();
- Set<Entry<String, Object>> setEntry = attrs.entrySet();
- for (Entry<String, Object> entryItem : setEntry) {
- Log.e(TAG,
- entryItem.getKey() + "====" + entryItem.getValue());
- }
- }
- }
- }
二、利用FeatureTable查詢,這是FeatureLayer特別的地方。比如在一個shp檔案中,有多個要素,我們知道有每個要素中有name這個屬性,怎樣快速查詢name為shanghai的要素呢?要用到FeatureTable的queryFeatures(QueryParameters query, CallbackListener<FeatureResult> callback)方法或者是queryIds(QueryParameters query, CallbackListener<long[]> callback)這個方法。
- private void queryTable() {
- FeatureTable featureTable = featureLayer.getFeatureTable();
- QueryParameters parameters = new QueryParameters();
- //查詢條件,有點像資料庫中的查詢
- parameters.setWhere("name = 'shanghai'");<span style="white-space:pre"> </span>Future<long[]> queryIds = featureTable.queryIds(parameters,new CallbackListener<long[]>() {
- @Override
- public void onError(Throwable arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onCallback(long[] arg0) {
- // TODO Auto-generated method stub
- }
- });
- try {
- long[] Ids = queryIds.get();
- if (Ids != null && Ids.length > 0) {
- for (int i = 0; i < Ids.length; i++) {
- Feature feature = featureLayer.getFeature(Ids[i]);
- Log.i(TAG, feature.getAttributes().get("name") + "");
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
這樣就可以快速查詢到某個具體的要素。
package com.huang.featuer; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import com.esri.android.map.FeatureLayer; import com.esri.android.map.MapView; import com.esri.android.map.event.OnSingleTapListener; import com.esri.core.geodatabase.ShapefileFeatureTable; import com.esri.core.map.CallbackListener; import com.esri.core.map.Feature; import com.esri.core.renderer.SimpleRenderer; import com.esri.core.symbol.SimpleFillSymbol; import com.esri.core.table.FeatureTable; import com.esri.core.tasks.query.QueryParameters; public class FeatureLayerActivity extends Activity implements OnClickListener { private static final String TAG = FeatureLayerActivity.class.getName(); private MapView mMapView; private String shpPath = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/test/test.shp"; private FeatureLayer featureLayer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMapView = (MapView) findViewById(R.id.mapView); findViewById(R.id.query_btn).setOnClickListener(this); initLayer(); mMapView.setOnSingleTapListener(new OnSingleTapListener() { public void onSingleTap(float x, float y) { handleSingleTap(x, y); } }); } private void initLayer() { try { featureLayer = new FeatureLayer(new ShapefileFeatureTable(shpPath)); featureLayer.setRenderer(new SimpleRenderer(new SimpleFillSymbol( Color.GREEN))); mMapView.addLayer(featureLayer); } catch (Exception e) { e.printStackTrace(); } } private void handleSingleTap(float x, float y) { long[] featureIds = featureLayer.getFeatureIDs(x, y, 8); if (featureIds != null && featureIds.length > 0) { for (long id : featureIds) { com.esri.core.map.Feature feature = featureLayer.getFeature(id); Map<String, Object> attrs = feature.getAttributes(); Set<Entry<String, Object>> setEntry = attrs.entrySet(); for (Entry<String, Object> entryItem : setEntry) { Log.e(TAG, entryItem.getKey() + "====" + entryItem.getValue()); } } } } public void onClick(View v) { switch (v.getId()) { case R.id.query_btn: query(); break; default: break; } } private void query() { FeatureTable featureTable = featureLayer.getFeatureTable(); QueryParameters parameters = new QueryParameters(); // 查詢條件,有點像資料庫中的查詢 parameters.setWhere("name = 'shanghai'"); Future<long[]> queryIds = featureTable.queryIds(parameters, new CallbackListener<long[]>() { public void onError(Throwable e) { // TODO Auto-generated method stub } public void onCallback(long[] objs) { // TODO Auto-generated method stub } }); try { long[] Ids = queryIds.get(); if (Ids != null && Ids.length > 0) { for (int i = 0; i < Ids.length; i++) { Feature feature = featureLayer.getFeature(Ids[i]); Log.i(TAG, feature.getAttributes().get("name") + ""); } } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }