ArcGIS Runtime SDK for Android Introduction (20): Display local map data - Shapefile file file

Keywords: Android SDK xml

This article mainly explains how to display local Shapefile files with ArcGIS Runtime SDK for Android.

Implementation steps:

1. Create an Android project

2. Adding Runtime SDK dependencies

The first two steps are omitted in this paper for beginners to refer to. Introduction to ArcGIS Runtime SDK for Android (1): First Map Application (2-D)

3. Adding permissions and OpenGL ES support

    <! - Internet access - >
    <uses-permission android:name="android.permission.INTERNET"/>
    <! - Read external storage permissions - >
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

4. Setting up the interface layout

Layout XML code:

    <!-- MapView control -->
    <com.esri.arcgisruntime.mapping.view.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    </com.esri.arcgisruntime.mapping.view.MapView>

5. Write code:

Ideas:

(1) Request permission to read and write files (Android version 6.0 and above).

(2) Get the Shapefile file file path.

(3) Create Shapefile Feature Table objects through paths and load them asynchronously.

(4) Create Feature Layer Element Layer Object through Shapefile Feature Table Object.

(5) Add Shapefile's Feature Layer element layer to the Operational Layers business layer in MapView map.

(6) Scale MapView to Shapefile for display.

Prepare: Create ArcGIS folder in the built-in SD card of Android device and put it into Shapefile folder with Shp file.

Steps:

(1) Variable preparation:

  // Log.e's Markup Variables
  private final static String TAG = MainActivity.class.getSimpleName();
  // MapView control variable
  private MapView mMapView;

(2) In the onCreate method:

    // Create a new map object displayed in MapView with a street map as its base
    mMapView = findViewById(R.id.mapView);
    ArcGISMap map = new ArcGISMap(Basemap.createStreetsVector());
    mMapView.setMap(map);
    //Request device read and write permission and load data
    requestReadPermission();

(3) Methodological support:

  // Request device read and write permission and load data
  private void requestReadPermission() {
    // Define request permissions
    String[] reqPermission = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE };
    int requestCode = 2;
    // In API version 23 or above, permissions need to be requested at runtime
    if (ContextCompat.checkSelfPermission(MainActivity.this,
        reqPermission[0]) == PackageManager.PERMISSION_GRANTED) {
      // Loading data
      featureLayerShapefile();
    } else {
      // Request permission
      ActivityCompat.requestPermissions(MainActivity.this, reqPermission, requestCode);
    }
  }

  // Processing privilege request response, user response after choosing privilege
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      // Loading data
      featureLayerShapefile();
    } else {
      // Reporting to user permission request rejected
      Toast.makeText(MainActivity.this, getResources().getString(R.string.read_permission_denied),
          Toast.LENGTH_SHORT).show();
    }
  }

  
  // Loading shapefile
  private void featureLayerShapefile() {
    // Get the local shapefile file
    String path = getSDCardPath()+"/ArcGIS/Shapefile/Aurora_CO_shp/Subdivisions.shp";
    ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(path);

    shapefileFeatureTable.loadAsync();
    shapefileFeatureTable.addDoneLoadingListener(() -> {
      if (shapefileFeatureTable.getLoadStatus() == LoadStatus.LOADED) {

        // Create a Feature Server file using the shapefile file file
        FeatureLayer shapefileFeatureLayer = new FeatureLayer(shapefileFeatureTable);
        // Add the shapefile layer to the business layer
        mMapView.getMap().getOperationalLayers().add(shapefileFeatureLayer);
        // Zoom in to the shapefile range
        mMapView.setViewpointAsync(new Viewpoint(shapefileFeatureLayer.getFullExtent()));
      } else {
        String error = "Shapefile feature table failed to load: " + shapefileFeatureTable.getLoadError().toString();
        Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
        Log.e(TAG, error);
      }
    });
  }

  // Getting SD Card Path
  public String getSDCardPath()
  {
    return Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator;
  }
  

6. Running APP: You can browse geographic data in Shapefile simply

Thank you, Mr. luq, for your guidance.

Posted by Elephant on Mon, 04 Feb 2019 13:18:17 -0800