Code sample: Creating custom shapes and objects

The following code sample demonstrates how you can create a MapSimplePolygon object and a GsImage object. The MapSimplePolygon has a transparent style applied to it that allows BlackBerry device users to see the map behind the object.

import net.rim.device.api.lbs.maps.*;
import net.rim.device.api.lbs.maps.ui.*;
import net.rim.device.api.lbs.maps.view.*;
import net.rim.device.api.lbs.maps.model.*;
import net.rim.device.api.lbs.maps.model.geospatial.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;

public class CustomObjectsDemo extends UiApplication 
    public CustomObjectsDemo() 
        pushScreen(new GSScreen());
    public static final void main( String[] args ) 
        new CustomObjectsDemo().enterEventDispatcher();

    public class CustomObjectsDemoScreen extends FullScreen 
        private RichMapField map;

        public CustomObjectsDemoScreen() 
            // Invokes the constructor for FullScreen to set the behavior
            // of the screen.
            super(FullScreen.DEFAULT_CLOSE | 
                    FullScreen.DEFAULT_MENU | 
                    FullScreen.VERTICAL_SCROLL | 
            // Creates a map and sets the center of the map.
            map = MapFactory.getInstance().generateRichMapField();                     
            map.getAction().setCenterAndZoom(new MapPoint(45.0, -75.0), 4);
            // Adds the geospatial objects to the map.

        private void addData() 
            // Creates and adds a MapSimplePolygon object.
            MapPoint[] points = new MapPoint[6];
            points[0] = new MapPoint(45.05, -75.0);
            points[1] = new MapPoint(45.0, -74.95);
            points[2] = new MapPoint(44.95, -74.975);
            points[3] = new MapPoint(44.95, -75.025);
            points[4] = new MapPoint(45.0, -75.05);
            points[5] = new MapPoint(45.05, -75.0);
            MapSimplePolygon poly = new MapSimplePolygon(points);
            map.getModel().add((Mappable) poly, "Polygon", true);
            // Creates and adds a GsImage object.
            GsImage gsImg = new GsImage();
            gsImg.setName("App World!");
            gsImg.setDescription("BlackBerry App World icon");
            map.getModel().add(gsImg, "Image", true);
            // Creates a style for the MapSimplePolygon class that
            // allows the user to see the map behind the object.
            StyleSet styles = map.getMapField().getDrawingStyles();
            Style transStyle = new Style();
            styles.addClassBasedStyle(MapSimplePolygon.class, transStyle);                                  

Was this information helpful? Send us your comments.