-
Notifications
You must be signed in to change notification settings - Fork 75
Editable CartoDB Layer
Nutiteq edited this page Jul 23, 2013
·
4 revisions
See Editable MapView for general steps of editable MapView and editable layer creation.
EditableCartoDbVectorLayer 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.
- Styles for all used geometry types
- 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 EditableCartoDbVectorLayer does not show MULTI elements* (e.g. MultiPolygons) which have more than 1 component.
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 here.
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;
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);
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();
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!";
EditableCartoDbVectorLayer cartoDbLayer = new EditableCartoDbVectorLayer(mapView.getLayers().getBaseProjection(), account, apiKey, querySql, insertSql, updateSql, deleteSql, multiGeometry, pointStyleSet, lineStyleSet, polygonStyleSet, this);
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