Implementing the map

TouristMap

This TouristMap class extends the HERE Maps API's MapCanvas class. The class draws the map and is complemented with MapComponents to add extra functionality. Some custom methods that interact with the attractions are also implemented.

The addMarkers method is used to add the markers representing the attractions to the map.

public void addMarkers() {
    Vector attrs = dataModel.getAttractions();
    if (attrs == attractions || attrs == null) {
        return;
    }
    attractions = attrs;
    //Remove all markers
    map.removeAllMapObjects();
    if (currentPositionCircle != null) {
        map.addMapObject(currentPositionCircle);
    }

    //Add a marker for each attraction
    for (int i = 0, size = attractions.size(); i < size; i++) {
        Attraction attr = (Attraction) attractions.elementAt(i);
        try {
            Image image = ImageLoader.getInstance().loadMapMarker(
                              attr.getId(), "/location.png", null);
            GeoCoordinate c = attr.getLocation();
            MapMarker marker = mapFactory.createMapMarker(c, image);
            marker.setAnchor(new Point(image.getWidth() / 2, image.
                                       getHeight()));
            map.addMapObject(marker);
        }
        catch (IOException e) {
        }
    }
}

The moveCenterToGuide method moves the map so that the center is at the coordinates specified in the tourist guide. This is used when the user wants to go to his or her location but the device does not have his or her coordinates.

private void moveCenterToGuide() {
    if (dataModel.getCurrentGuide() != null) {
        map.setCenter(dataModel.getCurrentGuide().getCenter());
    }
}

The goToAttraction method moves the center of the map to the given attraction.

public void goToAttraction(Attraction attr) {
    goToPosition(attr.getLocation());
}

The goToPosition method moves the center of the map to the given coordinates. If the coordinates are null, the method calls the moveCenterToGuide method.

private void goToPosition(GeoCoordinate pos) {
    if (pos != null) {
        map.setCenter(pos);
    }
    else {
        moveCenterToGuide();
    }
}

The goToMe method moves the center of the map to the user's current coordinates.

public void goToMe() {
    goToPosition(dataModel.getCurrentPosition());
}

The refreshCurrentPositionCircle method draws the circle representing the user's position on the map.

public void refreshCurrentPositionCircle() {
    GeoCoordinate currentPosition = dataModel.getCurrentPosition();
    if (currentPosition == null) {
        return;
    }
    int accuracy = dataModel.getAccuracy();
    if (currentPositionCircle == null) {
        currentPositionCircle = mapFactory.createMapCircle(accuracy,
                                currentPosition);
        currentPositionCircle.setColor(CIRCLE_COLOR);
        map.addMapObject(currentPositionCircle);
    }
    else {
        currentPositionCircle.setCenter(currentPosition);
        currentPositionCircle.setRadius(accuracy);
    }
}

TouristMapOverlays

The TouristMapOverlays class draws a back button on top of the map.

The pointer event methods check if the back button is pressed.

public final boolean pointerPressed(int x, int y) {
    if (hitsBackButton(x, y)) {
        backHasFocus = true;
        return true;
    }
    return false;
}

public final boolean pointerDragged(int x, int y) {
    if (backHasFocus) {
        backHasFocus = hitsBackButton(x, y);
        return true;
    }
    return false;
}

public final boolean pointerReleased(int x, int y) {
    if (backHasFocus && hitsBackButton(x, y)) {
        backHasFocus = false;
        Main.getInstance().closeMap();
        return true;
    }
    return false;
}

The back command is mapped to the right softkey. The key event methods check if the softkey is pressed.

public final boolean keyPressed(int keyCode, int i) {
    if (keyCode == KeyCodes.RIGHT_SOFTKEY) {
        backHasFocus = true;
        return true;
    }
    return false;
}

public final boolean keyReleased(int keyCode, int i) {
    backHasFocus = false;
    if (keyCode == KeyCodes.RIGHT_SOFTKEY) {
        Main.getInstance().closeMap();
        return true;
    }
    return false;
}