Skip to content

Editable CartoDB Layer

mtehver edited this page Apr 8, 2014 · 4 revisions

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

EditableCartoDbDataSource takes several special arguments:

  • Your CartoDB account data (account name and key)
  • SQL templates for select, insert, update and delete. Most probably you need to care about table name there only, otherwise it is same as in sample code. But you can add here extra logic, like updating several tables with one update, add PostGIS functions etc.
  • Multigeometry - saves data as MULTI* elements. Note that currently Nutiteq SDK does not keep multipolygon/line elements, during data reading it decomposes them to simple elements. Therefore EditableCartoDbDataSource does not show MULTI elements* (e.g. MultiPolygons) which have more than 1 component.

Note that EditableCartoDbDataSource is itself an abstract class and requires user to define actual methods for building element styles and labels. The example below uses same style for all elements.

Core code snippets - replace ALL_CAPS constants with your values :

// 1. Add base layer from CartoDB tile API. 
//   You can also use MapBox, MapQuest or any other base map source
      TMSMapLayer cartoDbTileLayer = new TMSMapLayer(mapView.getLayers().getBaseProjection(), 0, 19, 15,
         "http://YOUR_CARTODB_ACCOUNT.cartodb.com/tiles/YOUR_BASEMAP_TABLE/", "/", ".png");
      mapView.getLayers().setBaseLayer(cartoDbTileLayer);

// 2. Define styles for displayed objects. You could use just one of the 3 styles

      int minZoom = 5;	 // minimal zoom to show the vector data
      int color = Color.BLUE;
	
      final StyleSet<PointStyle> pointStyleSet = new StyleSet<PointStyle>();
      Bitmap pointMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.point);
      PointStyle pointStyle = PointStyle.builder().setBitmap(pointMarker).setSize(0.05f).setColor(color).setPickingSize(0.2f).build();
      pointStyleSet.setZoomStyle(minZoom, pointStyle);

      final StyleSet<LineStyle> lineStyleSet = new StyleSet<LineStyle>();
      LineStyle lineStyle = LineStyle.builder().setWidth(0.05f).setColor(color).build();
      lineStyleSet.setZoomStyle(minZoom, lineStyle);

      // polygons are a bit transparent.
      PolygonStyle polygonStyle = PolygonStyle.builder().setColor(color & 0x80FFFFFF).setLineStyle(lineStyle).build();
      final StyleSet<PolygonStyle> polygonStyleSet = new StyleSet<PolygonStyle>(null);
      polygonStyleSet.setZoomStyle(minZoom, polygonStyle);


// 3. Add editable vector layer. 
      String table = "YOUR_TABLE_TO_BE_EDITED";
      String account = "YOUR_CARTODB_ACCOUNT";
      String apiKey = "YOUR_ACCOUNT_API_KEY";
      // probably no need to configure below
      String querySql  = "SELECT cartodb_id, the_geom_webmercator, name FROM "+table+" WHERE the_geom_webmercator && ST_SetSRID('BOX3D(!bbox!)'::box3d, 3857)";
      String insertSql = "INSERT INTO "+table+" (the_geom) VALUES(ST_Transform(!geom!, 4326)) RETURNING cartodb_id";
      String updateSql = "UPDATE "+table+" SET the_geom=ST_Transform(!geom!, 4326), name=!name! WHERE cartodb_id=!id!";
      String deleteSql = "DELETE FROM "+table+" WHERE cartodb_id=!id!";

      EditableCartoDbDataSource dataSource =
        new EditableCartoDbDataSource (mapView.getLayers().getBaseProjection(), account, apiKey, querySql, insertSql, updateSql, deleteSql, multiGeometry) {
            @Override
            protected Label createLabel(Map<String, String> userData) {
                StringBuffer labelTxt = new StringBuffer();
                for (Map.Entry<String, String> entry : userData.entrySet()){
                    labelTxt.append(entry.getKey() + ": " + entry.getValue() + "\n");
                }
                return new DefaultLabel("Data:", labelTxt.toString());
            }

            @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;
            }
      };
      EditableCartoDbVectorLayer cartoDbLayer = new EditableGeometryLayer(dataSource);
      mapView.getLayers().addLayer(cartoDbLayer);

// 4. Save data.
// This is normally called from a UI event/button, and must run in separate thread (perhaps as AsyncTask)

      cartoDbLayer.saveChanges();

For detailed usage see EditableCartoDbMapActivity.java

Clone this wiki locally