Monday, January 17, 2011

Map Operation -- Pan Map

There’s two method normally ued to move map around, panTo move the center map to given latitude, longitude. panDirection (dx,dy) moves map to given dx,dy in pixel relative to current map location.  panTo trigges the whole map refresh, so if you need constantly update map (like tracking current location) ,panDirection(dx,dy) provides a better update performance.
      The following example can move map UP, DOWN, LEFT, RIGHT

public class MapPanMIDP extends MapDemoMIDP implements CommandListener {

    private Command mapUpCommand = new Command("Up", Command.OK, 1);
    private Command mapDownCommand = new Command("Down", Command.ITEM, 1);
    private Command mapLeftCommand = new Command("Left", Command.ITEM, 1);
    private Command mapRightCommand = new Command("Right", Command.ITEM, 1);

    public void startApp() {

        init();
        canvas.addCommand(mapUpCommand);
        canvas.addCommand(mapDownCommand);
        canvas.addCommand(mapLeftCommand);
        canvas.addCommand(mapRightCommand);
        canvas.setCommandListener(this);
        GeoLatLng center = new GeoLatLng(-31.948275, 115.857562);
        map.setCenter(center, 13, MapType.GOOGLEMAP);        Display.getDisplay(this).setCurrent(canvas);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == mapUpCommand) {
            map.panDirection(0, -32);

        } else if (c == mapDownCommand) {
            map.panDirection(0, 32);
        } else if (c == mapLeftCommand) {
            map.panDirection(-32, 0);
        } else if (c == mapRightCommand) {
            map.panDirection(32, 0);
        }

    }
}




No comments:

Post a Comment