📄 citymap.java
字号:
synchronized (mapListeners) { Enumeration listeners = mapListeners.elements(); while (listeners.hasMoreElements()) { ((MapListener)listeners.nextElement()).visitorPositionChanged(this); } } // deactivate active landmarks, which are now too away from the // visitor for (int i = 0; i < landmarks.length; ++i) { if (landmarks[i].isActive() && (newCoordinates.distance(landmarks[i].getLandmark().getQualifiedCoordinates()) > DEACTIVATION_RADIUS)) { activateLandmark(landmarks[i], false); try { // re-register a deactivated landmark to the location // provider, so we can get notified again locationProvider.addProximityListener(landmarks[i], landmarks[i].getLandmark().getQualifiedCoordinates(), ACTIVATION_RADIUS); } catch (LocationException e) { } } } } } /** * Changes the state of the visitor. A deactivated visitor doesn't change * his position. */ public synchronized void setVisitorActive(boolean active) { if (visitorActive != active) { visitorActive = active; synchronized (mapListeners) { Enumeration listeners = mapListeners.elements(); while (listeners.hasMoreElements()) { ((MapListener)listeners.nextElement()).visitorStateChanged(this); } } } } /** Returns the xy coordinates of the visitor. */ public synchronized int[] getVisitorXY(int[] dest) { if (dest == null) { dest = new int[2]; } dest[X] = visitorXY[X]; dest[Y] = visitorXY[Y]; return dest; } /** Returns the visitor icon based on his state. */ public Image getVisitorImage() { return images[visitorActive ? IMAGE_VISITOR_ON : IMAGE_VISITOR_OFF]; } /** Returns the map image. */ public Image getMapImage() { return images[IMAGE_MAP]; } /** Returns the set of the map landmarks. */ public MapLandmark[] getMapLandmarks() { return landmarks; } /** Registers a map listener. */ void addMapListener(MapListener listener) { synchronized (mapListeners) { mapListeners.addElement(listener); } } /** Unregisters a map listener. */ void removeMapListener(MapListener listener) { synchronized (mapListeners) { mapListeners.removeElement(listener); } } /** * Converts from the given latitude / longitude coordinates to the map * xy coordinates. */ public int[] convertCoordinatesToXY(int[] dest, Coordinates coords) { if (dest == null) { dest = new int[2]; } double leftLatitude = topLeftCoordinates.getLatitude(); double rightLatitude = bottomRightCoordinates.getLatitude(); double topLongitude = topLeftCoordinates.getLongitude(); double bottomLongitude = bottomRightCoordinates.getLongitude(); double normalizedX = (coords.getLatitude() - leftLatitude) / (rightLatitude - leftLatitude); double normalizedY = (coords.getLongitude() - topLongitude) / (bottomLongitude - topLongitude); dest[X] = (int)(normalizedX * images[IMAGE_MAP].getWidth()); dest[Y] = (int)(normalizedY * images[IMAGE_MAP].getHeight()); return dest; } /** * Converts from the given map xy coordinates to the latitude / longitude * coordinates. */ public Coordinates convertXYToCoordinates(Coordinates dest, int[] xy) { double latitude = topLeftCoordinates.getLatitude() + (((bottomRightCoordinates.getLatitude() - topLeftCoordinates.getLatitude()) * xy[X]) / images[IMAGE_MAP].getWidth()); double longitude = topLeftCoordinates.getLongitude() + (((bottomRightCoordinates.getLongitude() - topLeftCoordinates.getLongitude()) * xy[Y]) / images[IMAGE_MAP].getHeight()); float altitude = topLeftCoordinates.getAltitude(); if (dest == null) { dest = new Coordinates(latitude, longitude, altitude); } else { dest.setLatitude(latitude); dest.setLongitude(longitude); dest.setAltitude(altitude); } return dest; } /** * A method which is called by the location provider when the current * location is changed. */ public synchronized void locationUpdated(LocationProvider provider, Location location) { if (disabled) { return; } Coordinates coordinates = location.getQualifiedCoordinates(); double latitude = coordinates.getLatitude(); double longitude = coordinates.getLongitude(); double lat0 = topLeftCoordinates.getLatitude(); double lat1 = bottomRightCoordinates.getLatitude(); double lon0 = topLeftCoordinates.getLongitude(); double lon1 = bottomRightCoordinates.getLongitude(); if ((((latitude >= lat0) && (latitude <= lat1)) || ((latitude >= lat1) && (latitude <= lat0))) && (((longitude >= lon0) && (longitude <= lon1)) || ((longitude >= lon1) && (longitude <= lon0)))) { setVisitorCoordinates(coordinates); } } /** * A method which is called by the location provider when its state changes * (for example, when its services are temporary unavailable). */ public synchronized void providerStateChanged(LocationProvider provider, int newState) { if (disabled) { return; } setVisitorActive(newState == LocationProvider.AVAILABLE); } /** * Sets the city map to the disabled state. In the disabled state it ignores * all notifications from the location provider. */ public synchronized void disable() { disabled = true; } /** * Sets the city map to the enabled state. */ public synchronized void enable() { disabled = false; } /** The final unregistration. */ public synchronized void cleanup() { for (int i = 0; i < landmarks.length; ++i) { locationProvider.removeProximityListener(landmarks[i]); } locationProvider.setLocationListener(null, -1, -1, -1); } /** * This class extends the MapLandmark class to support getting of proximity * events from a location provider. It ignores the monitoring state changed * events and delegates the proximity events to the CityMap instance. */ private class MyMapLandmark extends MapLandmark implements ProximityListener { private int index; public MyMapLandmark(Landmark landmark, int x, int y, Image image) { super(landmark, x, y, image); } public void setActive(boolean active) { this.active = active; } public void monitoringStateChanged(boolean isMonitoringActive) { } public void proximityEvent(Coordinates coordinates, Location location) { activateLandmark(this, true); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -