Skip to content

Polygons with 3D height

mtehver edited this page Mar 6, 2013 · 5 revisions

Following code snippet adds one polygon with height, we call them 3D polygons. It is fine to render basic buildings boxes. Of course the photo-realistic building models you need more complex tools. We have these too but the problem is from where to get the content. OSM does not have it. So if you have content and need real 3D model tools - please drop an email to the email list or [email protected]

        // ++ add one 3DPolygon object
        // first define coordinates, convert them to suitable ArrayList
        double[][] coords = {{-122.40646892021,37.785019682367},{-122.40554410462,37.784292332704},{-122.40544277466,37.784372558055},{-122.40500646292,37.784024535593},{-122.40607402081,37.783181380135},{-122.40653512604,37.783549640611},{-122.40676940667,37.783737355482},{-122.40715774837,37.783430153173},{-122.40782501696,37.783947859678}};
        ArrayList<MapPos> mapPoses = new ArrayList<MapPos>();
        for(double[] coord:coords){
            mapPoses.add(mapLayer.getProjection().fromWgs84((float)coord[0],(float)coord[1]));
        }

        // now define style for the buildings. Usually just color
        Polygon3DStyle polygon3DbuildingStyle = Polygon3DStyle.builder().setColor(Color.GREEN | 0x40ffffff).build();
        
        // now you can make real building object. Height is here empirically found value which gives nice result.
        Polygon3D building3D = new Polygon3D(mapPoses, null, 0.5f, new DefaultLabel("Westfield San Francisco Centre"), polygon3DbuildingStyle, null);
        // create layer and add object to it, add layer to map
        Polygon3DLayer polygon3DLayer = new Polygon3DLayer(mapLayer.getProjection());
        polygon3DLayer.add(building3D);
        mapView.getLayers().addLayer(polygon3DLayer);

Screenshot with the 3D building polygon

Clone this wiki locally