arcgis for Android 100.2 加载shp

几个月前就测试了arcgis for Android 100.2。之前也更新了几篇arcgis 相关的。现在继续。

arcgis for Android 100.1 基本操作(缩小放大旋转定位)

arcgis for Android 100.1 在线加载天地图和谷歌地图

1、显示效果



2、实现代码

package com.arcgis.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;

import com.arcgis.R;
import com.arcgis.until.FilePathManage;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchStyle;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 数据量大的时候,加载速度比较慢,有时候还没加载出来,一直在找
 * 最后又pc上软件知道shp的位置,比较好测试
 *
 * 还有shp 必须有least three files (.shp, .shx, .dbf) 如果有prj文件那就更好。位置准确
 *
 * 受之前10的版本影响,我一直在找怎么设置投影,其实是多余的。不用设置,接口都没有。里面已经处理好
 *
 */
public class ArcgisShpMapActivity extends AppCompatActivity {
    private MapView mMapView;
    private ShapefileFeatureTable shapefileFeatureTable;
    private ArcGISMap arcGISMap;
    private FeatureLayer featureLayer;
    private SketchEditor mainSketchEditor;
    private SketchStyle mainSketchStyle;

    private String shpName = "/DLJX.shp";
//    private String shpName = "/未命名_20171130163924.shp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mMapView = (MapView) findViewById(R.id.map_view);


        showShapefile();
//        openShp();
    }

    //加载shp方式一
    private void openShp() {
        // 构建ShapefileFeatureTable,引入本地存储的shapefile文件
        shapefileFeatureTable = new ShapefileFeatureTable(
                FilePathManage.GetInstance().getShpDirectory() + shpName);
        shapefileFeatureTable.loadAsync();
        // 构建featureLayerr
        featureLayer = new FeatureLayer(shapefileFeatureTable);
        // 设置Shapefile文件的渲染方式
        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 1.0f);
        SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.YELLOW, lineSymbol);
        SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
        featureLayer.setRenderer(renderer);
        // 添加到地图的业务图层组中
        arcGISMap = new ArcGISMap(Basemap.createTopographic());
        arcGISMap.getOperationalLayers().add(featureLayer);
        // set the map to the map view
        mMapView.setMap(arcGISMap);
    }

    //加载shp方式二
    private void showShapefile() {
        mMapView.setAttributionTextVisible(false);
        arcGISMap = new ArcGISMap(new Basemap().createImageryWithLabelsVector());
        mMapView.setMap(arcGISMap);
        final ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(
                FilePathManage.GetInstance().getShpDirectory() + shpName);
        shapefileFeatureTable.loadAsync();
        shapefileFeatureTable.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                GeometryType gt = shapefileFeatureTable.getGeometryType();
                String name = shapefileFeatureTable.getTableName();
                featureLayer = new FeatureLayer(shapefileFeatureTable);
                if (featureLayer.getFullExtent() != null) {
                    mMapView.setViewpointGeometryAsync(featureLayer.getFullExtent());
                } else {
                    featureLayer.addDoneLoadingListener(new Runnable() {
                        @Override
                        public void run() {
                            mMapView.setViewpointGeometryAsync(featureLayer.getFullExtent());
                        }
                    });
                }
                arcGISMap.getOperationalLayers().add(featureLayer);
            }
        });


        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 1.0f);
        SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.YELLOW, lineSymbol);
        SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
        featureLayer.setRenderer(renderer);

    }

    public void queryShp2(){
        //单击shp 进行查询
        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean  onSingleTapConfirmed(MotionEvent v) {
                android.graphics.Point screenPoint=new android.graphics.Point(Math.round(v.getX()), Math.round(v.getY()));
                Point clickPoint = mMapView.screenToLocation(screenPoint);
                QueryParameters query = new QueryParameters();
                query.setGeometry(clickPoint);// 设置空间几何对象
                FeatureTable mTable = featureLayer.getFeatureTable();//得到查询属性表
                final ListenableFuture featureQueryResult
                        = mTable.queryFeaturesAsync(query);
                featureQueryResult.addDoneListener(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            FeatureQueryResult featureResul = featureQueryResult.get();
                            for (Object element : featureResul) {
                                if (element instanceof Feature) {
                                    Feature mFeatureGrafic = (Feature) element;
                                    Map mQuerryString = mFeatureGrafic.getAttributes();
                                    for(String key : mQuerryString.keySet()){
                                        Log.i("Show"+key,String.valueOf(mQuerryString.get(key)));
                                    }
                                }
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                });
                return true;
            }
        });
    }


    //查询shp2
    public void queryShp() {
        try {
            mainSketchEditor = new SketchEditor();
            mainSketchStyle = new SketchStyle();
            mainSketchEditor.setSketchStyle(mainSketchStyle);
            mMapView.setSketchEditor(mainSketchEditor);
            mMapView.setOnTouchListener(
                    new DefaultMapViewOnTouchListener(this, mMapView) {
                        @Override
                        public boolean onSingleTapConfirmed(MotionEvent e) {
                            Point clickPoint = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                            int tolerance = 1;
                            double mapTolerance = tolerance * mMapView.getUnitsPerDensityIndependentPixel();
                            Envelope envelope = new Envelope(clickPoint.getX() - mapTolerance, clickPoint.getY() - mapTolerance, clickPoint.getX() + mapTolerance, clickPoint.getY() + mapTolerance, mMapView.getSpatialReference());
                            QueryParameters query = new QueryParameters();
                            query.setGeometry(envelope);
                            query.setSpatialRelationship(QueryParameters.SpatialRelationship.WITHIN);
                            final ListenableFuture future = featureLayer.selectFeaturesAsync(query, FeatureLayer.SelectionMode.NEW);
                            future.addDoneListener(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        FeatureQueryResult result = future.get();
                                        //mainShapefileLayer.getFeatureTable().deleteFeaturesAsync(result);
                                        Iterator iterator = result.iterator();

                                    } catch (Exception e) {
                                        e.getCause();
                                    }
                                }
                            });

                            return super.onSingleTapConfirmed(e);
                        }
                    }
            );
        } catch (Exception e) {
            e.getCause();
        }
    }

    //增加
    public void addShp() {
        // 调用isEditable和canAdd方法判断文件是否支持编辑操作,是否可添加要素;否,则抛出信息
        if (shapefileFeatureTable.isEditable() && shapefileFeatureTable.canAdd()) {

            // 构建新增几何
            Point mapPoint = new Point(140.0, 39.0, SpatialReference.create(4326));

            // 构建待增加的Feature对象,设置几何,设置属性
            Feature feature = shapefileFeatureTable.createFeature();
            feature.setGeometry(mapPoint);
            feature.getAttributes().put("NAME", "测试点");

            // 调用addFeatureAsync方法增加要素
            final ListenableFuture addFeatureOper = shapefileFeatureTable.addFeatureAsync(feature);

            // 在操作完成的监听事件中判断操作是否成功
            addFeatureOper.addDoneListener(new Runnable() {
                @Override
                public void run() {
                    try {
                        addFeatureOper.get();
                        if (addFeatureOper.isDone()) {
                            Log.i("ShapefileEdit:", "Feature added!");
                        }
                    } catch (InterruptedException interruptedExceptionException) {
                        // 处理异常
                    } catch (ExecutionException executionException) {
                        // 处理异常
                    }
                }
            });
        } else {
            Log.i("ShapefileEdit:", "The Shapefile cann't be edited");
        }
    }

    protected void onResume() {
        super.onResume();
        mMapView.resume();
    }

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


}
测试的注意事项,自已看啦
* 数据量大的时候,加载速度比较慢,有时候还没加载出来,一直在找
* 最后又pc上软件知道shp的位置,比较好测试
*
* 还有shp 必须有least three files (.shp, .shx, .dbf) 如果有prj文件那就更好。位置准确
*
* 受之前10的版本影响,我一直在找怎么设置投影,其实是多余的。不用设置,接口都没有。里面已经处理好

3、权限

对于新手开发,我每次都强调权限

    
    
    

你可能感兴趣的:(Android,移动开发,Android,常用开发技术)