The TouristMap
class extends the MapCanvas
class of the Nokia Maps API. The class draws the map and uses the TouristMapOverlays
component for extra functionality. The
class also implements custom methods that interact with the attractions.
The addMarkers
method adds the markers representing
the attractions to the map.
public final 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 their
location but the device does not have their 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 final 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 final void goToMe() { goToPosition(dataModel.getCurrentPosition()); }
The refreshCurrentPositionCircle
method draws the circle representing the user's position on the
map.
public final 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); } }
The TouristMapOverlays
class draws a back button
on top of the map.
The pointer event methods check if the back button is pressed.
/** * @see EventListener#pointerDragged(int, int) */ public final boolean pointerDragged(int x, int y) { if (backHasFocus) { backHasFocus = hitsBackButton(x, y); return true; } return false; } /** * @see EventListener#pointerPressed(int, int) */ public final boolean pointerPressed(int x, int y) { if (hitsBackButton(x, y)) { backHasFocus = true; return true; } return false; } /** * @see EventListener#pointerReleased(int, int) */ 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.
/** * @see EventListener#keyPressed(int, int) */ public final boolean keyPressed(int keyCode, int i) { if (keyCode == KeyCodes.RIGHT_SOFTKEY) { backHasFocus = true; return true; } return false; } /** * @see EventListener#keyReleased(int, int) */ public final boolean keyReleased(int keyCode, int i) { backHasFocus = false; if (keyCode == KeyCodes.RIGHT_SOFTKEY) { Main.getInstance().closeMap(); return true; } return false; } /** * @see EventListener#keyRepeated(int, int, int) */ public final boolean keyRepeated(int i, int i1, int i2) { return false; }