Skip to content
jaakla edited this page Sep 24, 2014 · 12 revisions

To add Vector objects you need to:

  1. Create GeometryLayer and add it to MapView
  2. Define styles for objects
  3. Define objects
  4. Add objects to your layer

1. Define styles

        // define minimum zoom for vector style visibility. If 0, then objects are visible with any zoom.
        int minZoom = 10;

        // load bitmaps for vector elements. You can get the images from Hellomap3D project res/drawable
        // these are simple anti-aliased bitmaps which can change colour, should be good for most cases
        Bitmap pointMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.point);
        Bitmap lineMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.line);

        // set styles for all 3 object types: point, line and polygon
        StyleSet<PointStyle> pointStyleSet = new StyleSet<PointStyle>();
        PointStyle pointStyle = PointStyle.builder().setBitmap(pointMarker).setSize(0.1f).setColor(Color.GREEN).build();
        pointStyleSet.setZoomStyle(minZoom,pointStyle);

        // We reuse here pointStyle for Line. This is used for line caps, useful for nicer polylines
        // Also do not forget to set Bitmap for Line. This allows to have fancy styles for lines.
        StyleSet<LineStyle> lineStyleSet = new StyleSet<LineStyle>();
        lineStyleSet.setZoomStyle(minZoom, LineStyle.builder().setBitmap(lineMarker).setWidth(0.1f).setColor(Color.GREEN).setPointStyle(pointStyle).build());
        PolygonStyle polygonStyle = PolygonStyle.builder().setColor(Color.BLUE).build();
        StyleSet<PolygonStyle> polygonStyleSet = new StyleSet<PolygonStyle>(null);
        polygonStyleSet.setZoomStyle(minZoom, polygonStyle);

2. Create layer and add to map

   GeometryLayer geomLayer = new GeometryLayer(proj);
   mapView.getLayers().addLayer(geomLayer);

3. Define vector objects and add to Layer

a) Create Point and add to layer

    geomLayer.add(new Point(this.proj.fromWgs84(20.465881f,44.809122f),  new DefaultLabel("Zagreb"), pointStyle, null));

b) Create Lines with some predefined coordinates

        // define 2 lines as WGS84 coordinates in an array.
        double[][][] lCoordss = {{{0,0},{0,51},{22,51}},{{2,2},{2,50},{24,50}}};

        // create two lines with these coordinates
        // if your line is in basemap projection coordinates, no need to use conversion
        for(double[][] lCoords:lCoordss){
            ArrayList<MapPos> lPoses =  new ArrayList<MapPos>();
            for(double[] coord:lCoords){
                lPoses.add(this.proj.fromWgs84((float)coord[0],(float)coord[1]));
            }
            geomLayer.add(new Line(lPoses, new DefaultLabel("Line"), lineStyleSet, null));
        }

c) Create and add Polygon

It is very similar to Line case, except first and last coordinate must match, and you need to use PolygonStyle instead of LineStyle. Polygon can also have one or more holes.

        // add polygon with a hole. Inner hole coordinates must be entirely within.
        double[][] pCoordsOuter = {{0,0},{0,51},{22,51},{0,0}}; // outer ring
        double[][] pCoordsInner = {{1,10},{1,50},{10,50},{1,10}}; // inner ring

        ArrayList<MapPos> outerPoses =  new ArrayList<MapPos>();
        for(double[] coord:pCoordsOuter){
          outerPoses.add(this.proj.fromWgs84((float)coord[0],(float)coord[1]));
        }

        ArrayList<MapPos> innerPoses =  new ArrayList<MapPos>();
        for(double[] coord:pCoordsInner){
            innerPoses.add(this.proj.fromWgs84((float)coord[0],(float)coord[1]));
        }
        // we need to create List of holes, as one polygon can have several holes
        // here we have just one. You can have nil there also.
        List<List<MapPos>> holes =  new ArrayList<List<MapPos>>();
        holes.add(innerPoses);
        
        geomLayer.add(new Polygon(outerPoses, holes, new DefaultLabel("Polygon"), polygonStyleSet, null));

d) Add Texts to the map

        TextLayer textLayer = new TextLayer(proj);
        // note that TextStyle has many more parameters, see javadoc
        TextStyle smallTextStyle16 = TextStyle.builder()
                .setSize(36)
                .setAllowOverlap(false)
                .setOrientation(TextStyle.GROUND_ORIENTATION)
                .setAnchorY(TextStyle.CENTER)
                .setColor(Color.BLACK)
                .setOffset3DZ(0.01f)
                .build();
        Text text = new Text(proj.fromWgs84(20.466027f, 44.810537f),
                "here comes a text", smallTextStyle16, null);
        textLayer.add(text);

        mapView.getLayers().addLayer(textLayer);
Clone this wiki locally