Code sample: Customizing the appearance of mappable objects

The following code sample demonstrates how to define a base style, class style, and an ID style, and apply the styles to the style set for a map.

import net.rim.device.api.ui.*;
import net.rim.device.api.lbs.maps.MapConstants;
import net.rim.device.api.lbs.maps.MapFactory;
import net.rim.device.api.lbs.maps.model.MapDataModel;
import net.rim.device.api.lbs.maps.model.MapLocation;
import net.rim.device.api.lbs.maps.ui.RichMapField;
import net.rim.device.api.lbs.maps.view.Style;
import net.rim.device.api.lbs.maps.view.StyleSet;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.container.FullScreen;

public class StyleSetApp extends UiApplication 
{
    public StyleSetApp() 
    {
        pushScreen(new StyleSetScreen());
    }
    
    public static void main(String[] args) 
    {
        new StyleSetApp().enterEventDispatcher();
    }
    
    public class StyleSetScreen extends FullScreen 
    {
        private RichMapField map;
        private MapDataModel model;
        private StyleSet styles;
        
        public StyleSetScreen() 
        {
            // Invoke the constructor for FullScreen to define the behavior of the
            // screen.
            super(FullScreen.DEFAULT_CLOSE | FullScreen.DEFAULT_MENU);
            
            // Create and add a map.
            map = MapFactory.getInstance().generateRichMapField();
            map.getAction().disableOperationMode( MapConstants.MODE_SHARED_FOCUS );
            map.getAction().setCenterLatLon( 44.9500, -75.0000 );
            map.getAction().setZoom( 4 );
            add(map);
            
            // Retrieve the data model and style set for the map.
            model = map.getModel();
            styles = map.getMapField().getDrawingStyles();
            
            // Define a base style for the entire map.
            Style baseStyle = new Style();
            baseStyle.setLabelFontFamilyName("arial");
            styles.setBaseStyle( baseStyle );

            // Define a style for the MapLocation class.
            Style classStyle = new Style();
            classStyle.setLabelFillColor( Color.BLACK );
            classStyle.setLabelFontColor( Color.WHITE );
            classStyle.setEdgeColor( Color.WHITE );
            styles.addClassBasedStyle( MapLocation.class, classStyle );

            // Define a style for an ID.
            Style idStyle = new Style();
            idStyle.setEdgeOpacity(15);
            idStyle.setFillOpacity(15);
            idStyle.setLabelFillOpacity(50);
            styles.addIdBasedStyle("ID_1", idStyle);
        
            // Create objects that demonstrate the MapLocation class style.
            MapLocation wes = new MapLocation( 45.0000, -75.0100, "Wes", 
                    "Displays a class style" );
            model.add( wes );
            
            MapLocation jenn = new MapLocation( 45.0000, -74.9900, "Jenn", 
                    "Displays a class style" );
            model.add( jenn );

            // Create objects that demonstrate the ID style.
            MapLocation mike = new MapLocation( 44.9900, -75.0100, "Mike", 
                    "Displays an ID style" );
            model.add( mike );
            mike.setStyleId( "ID_1" );
  
            MapLocation jamie = new MapLocation( 44.9900, -74.9900, "Jamie", 
                    "Displays an ID style" );
            model.add( jamie );
            jamie.setStyleId( "ID_1" );       
        }  
    }       
}

Was this information helpful? Send us your comments.