-
Notifications
You must be signed in to change notification settings - Fork 75
Cartodb layer
Nutiteq edited this page Aug 8, 2014
·
9 revisions
CartoDB provides nice geodata server as a service, with data storage, rich API, a lot of nice web-based client solutions and content management. You can keep your data in CartoDB and use Nutiteq SDK as Android native client solution. CartoDB provides several APIs, and we support following:
- SQL API enables to use SQL (with PostGIS extension) to request vector data. As with every vector data you need to define styles. We define here styles for all possible geometry types, so the layer works for Points, Polygons and Lines. As you see the vector data is interactive - you can click on objects and will see Labels.
// 1. Define styles for all possible geometry types
int color = Color.BLUE;
int minZoom = 5;
final Bitmap pointMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.point);
final StyleSet<PointStyle> pointStyleSet = new StyleSet<PointStyle>();
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.04f).setColor(Color.WHITE).build();
lineStyleSet.setZoomStyle(minZoom, lineStyle);
final StyleSet<PolygonStyle> polygonStyleSet = new StyleSet<PolygonStyle>(null);
PolygonStyle polygonStyle = PolygonStyle.builder().setColor(0xFFFF6600 & 0x80FFFFFF).setLineStyle(lineStyle).build();
polygonStyleSet.setZoomStyle(minZoom, polygonStyle);
// 2. define CartoDB connection parameters, table and SQL query
String account = "nutiteq";
String table = "tm_world_borders"; // kihelkonnad_1897, maakond_20120701
String columns = "cartodb_id,name,iso2,pop2005,area,the_geom_webmercator"; // NB! always include cartodb_id and the_geom_webmercator
int limit = 5000; // max number of objects
String sql = "SELECT "+columns+" FROM "+table+" WHERE the_geom_webmercator && ST_SetSRID('BOX3D(!bbox!)'::box3d, 3857) LIMIT "+limit;
// 3. Define DataSource
CartoDbDataSource cartoDataSource = new CartoDbDataSource(mapView.getLayers().getBaseLayer().getProjection(), account, sql) {
@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;
}
};
// 4. Define and add layer
GeometryLayer cartoLayerTrunk = new GeometryLayer(cartoDataSource);
mapView.getLayers().addLayer(cartoLayerTrunk);
- Map Tiles - Currently you can use standard RasterLayer() for this. CartoDB tile URL format is http://{account}.cartodb.com/tiles/{table_name}/{z}/{x}/{y}.png. Update: Since CartoDB 2.0 there is change on the way tiles are served, so before accessing a tile you need to do a POST with the tiles configuration (essentially the SQL and CartoCSS) and the server returns the url for the tiles. However, currently the simple tile request works also.
The Estonian historical regions as overlay layer in Nutiteq SDK would be:
RasterDataSource dataSource = new HTTPRasterDataSource(new EPSG3857(), 1, 19, "http://nutiteq.cartodb.com/tiles/kihelkonnad_1897/{zoom}/{x}/{y}.png");
RasterLayer mapLayer = new RasterLayer(dataSource, 3);
mapView.getLayers().setBaseLayer(mapLayer);
- You can even have update of CartoDB layer - insert, modify and delete object geometries. See Editable MapView for details