Skip to content

Editable Spatialite Layer

mtehver edited this page Apr 8, 2014 · 8 revisions

See Editable MapView for general steps of editable MapView and editable layer creation.

EditableSpatialiteLayer has following special arguments:

  • Spatialite connection helper instance
  • Geometry column name
  • Additional table columns to be added to userData

The class itself is abstract and requires user to define own implementations for style and label factory methods.

This layer uses custom SQLite native library, not Android built-in SQLite which does not support spatial operations. Following steps are needed to install this to your project:

  1. copy libs/ARCHITECTURE/libjsqlite.so file from AdvancedMap3D project to your project, same folder. Usually armeabi is the only architecture you need, but there are also mips and x86 builds.
  2. copy all src/test/java/jsqlite .java files to your project source folder

Core code snippets - replace ALL_CAPS constants with your values:

    // 1. create stylesets. You may need just one of them
    final StyleSet<PointStyle> pointStyleSet = new StyleSet<PointStyle>();
    PointStyle pointStyle = PointStyle.builder().setColor(Color.GREEN).setSize(0.2f).build();
    pointStyleSet.setZoomStyle(0, pointStyle);

    final StyleSet<LineStyle> lineStyleSet = new StyleSet<LineStyle>();
    LineStyle lineStyle = LineStyle.builder().setWidth(0.1f).setColor(Color.BLUE).build();
    lineStyleSet.setZoomStyle(0, lineStyle);

    final StyleSet<PolygonStyle> polygonStyleSet = new StyleSet<PolygonStyle>();
    PolygonStyle polygonStyle = PolygonStyle.builder().setColor(Color.BLUE | Color.GREEN).build();
    polygonStyleSet.setZoomStyle(0, polygonStyle);

    // 2. define data source parameters
    String dbPath = "YOUR_DB_FILE_PATH";
    String tableName = "YOUR_TABLE_TO_BE_EDITED";
    String geomColumn = "YOUR_GEOMETRY_COLUMN_NAME";
    // define array of columns what you need later to be shown, usually objects have "name" column
    String[] userDataColumns = new String[]{"name"};

    // 3. create editable layer. 
    EditableSpatialiteDataSource dataSource = new EditableSpatialiteDataSource(
       new EPSG3857(), new SpatialLiteDbHelper(dbPath),
       tableName, geomColumn, userDataColumns, null) {
            @Override
            protected Label createLabel(Map<String, String> userData) {
                return null; // no label
            }

            @Override
            protected StyleSet<PointStyle> createPointStyleSet(Map<String, String> userData, int zoom) {
                return pointStyleSet;
            }

            @Override
            protected StyleSet<LineStyle> createLineStyleSet(Map<String, String> userData, int zoom) {
                return lineStyleSet;
            }

            @Override
            protected StyleSet<PolygonStyle> createPolygonStyleSet(Map<String, String> userData, int zoom) {
                return polygonStyleSet;
            }
    };
    EditableGeometryLayer dbEditableLayer = new EditableGeometryLayer(dataSource);
    mapView.getLayers().addLayer(dbEditableLayer);

    // 4. It is often useful to zoom map automatically to data extent
    Envelope extent = dbEditableLayer.getDataExtent();
    mapView.setBoundingBox(new Bounds(extent.minX, extent.maxY, extent.maxX, extent.minY), false);

See EditableSpatialiteMapActivity.java for detailed sample

Clone this wiki locally