📄 mapview.java
字号:
// maxLon = mapData.getMaxLon() - shiftLon;// } else {// oLon = getOriginLon() + shiftLon;// maxLon = mapData.getMaxLon() + shiftLon;// }// } else { Point origin = getOrigin(); Rectangle bounds = holder.getBounds(); Rectangle clientArea = getClientArea(); Point newOrigin = new Point(origin.x, origin.y); newOrigin.x += x; newOrigin.y += y; if (newOrigin.x >= bounds.width -clientArea.width) newOrigin.x = bounds.width -clientArea.width; if (newOrigin.y >= bounds.height-clientArea.height) newOrigin.y = bounds.height-clientArea.height; if (newOrigin.x < 0) newOrigin.x = 0; if (newOrigin.y < 0) newOrigin.y = 0; if (newOrigin.x == origin.x && newOrigin.y == origin.y) return; setOrigin(newOrigin);// } } public void setOrigin(Point origin) { if ((getStyle() & SWT.H_SCROLL) != 0) super.setOrigin(origin); else holder.setLocation(-origin.x, -origin.y); } public void onDispose() { /*Enumeration i = allGlyphs.elements(); while(i.hasMoreElements()) { Glyph g = (Glyph)i.nextElement(); g.dispose(); } allGlyphs.clear(); if (mapImageGlyph != null) mapImageGlyph.dispose(); if (mapImage != null) mapImage .dispose(); if (iconCursor != null) iconCursor.dispose(); if (iconTextBg != null) iconTextBg.dispose();*/ //holder.getChild().dispose(); mapImageGlyph = null; mapImage = null; iconCursor = null; iconTextBg = null; mapData = null; } protected void clearPlaceIcons() { for(int i = 0; i < placeIcons.size(); i++) { ((MapIcon)placeIcons.elementAt(i)).dispose(); } placeIcons.removeAllElements(); } public GlyphHolder getHolder() { return holder; } public Point getMapSize() { return holder.getSize(); } public void setMapData(MapBacking d) { if (mapImage != null) mapImage.dispose(); if (mapImageGlyph != null) mapImageGlyph.dispose(); zoom=1.0; //zoomIndex = 3; if(mapHistory.size() > 0) { if(mapData != null && !mapData.getName().equals(d.getName())) { // if this is a totally new map, and not just // a variation on an existing tigermap, clear out // the tiger zoom history mapHistory.removeAllElements(); } } mapData = d; if(d instanceof BitmapMapBacking) {//ALM if(tigerOverlay != null) tigerOverlay.dispose();// tigerMode = false;// tigerOverlay = null; mapImageGlyph = new GlyphImage(holder, SWT.NONE); mapImage = new Image(getDisplay(), ((BitmapMapBacking)mapData).getImageResource()); mapImageGlyph.setImage(mapImage, 0, 0);//ALM } else {// tigerMode = true;// if(tigerOverlay != null) tigerOverlay.dispose();// try {// tigerOverlay = new TigerOverlay(this, ((TigerMapBacking)d).getTigerLineData());// } catch (IOException e) {// // TODO Auto-generated catch block// e.printStackTrace();// } catch (InvalidRecordException e) {// // TODO Auto-generated catch block// e.printStackTrace();// }// mapImage = null;// mapImageGlyph = null; } this.resizeHolder(); setPlaceSets(places); // notify overlays Enumeration i = overlays.elements(); while(i.hasMoreElements()) { MapViewOverlay overlay = (MapViewOverlay)i.nextElement(); overlay.mapChanged(d); } //holder.redraw(); this.getParent().redraw(); System.gc(); } public boolean isInTigerMode() { return false; //ALM tigerMode; } private void resizeHolder() {//ALM if(tigerMode) {// // don't want to get a squished looking map, so allow scrollbars// // when the view is disproportionately thin or fat// int width = getBounds().width;// int height = getBounds().height;// if(width > (height * 2)) height = (int)((double)width * 0.75);// else if(height > (width * 2)) width = (int)((double)height * 0.75);// holder.setSize(width, height);// } else { Rectangle r = mapImage.getBounds(); holder.setSize((int)(r.width * zoom + 0.5), //zoomValues[zoomIndex] + 0.5), (int)(r.height * zoom + 0.5)); //zoomValues[zoomIndex] + 0.5)); }//ALM } /** * The placeSets are a Hashtable where place set names * key to Hashtables where place names key to PlaceBackings */ public void setPlaceSets(Hashtable placeSets) { // to draw the map, the set that a place belongs // to is irrelevant. Therefore the structure is flattened //HashtableSet temp = new HashtableSet(); if(mapData == null) return; clearPlaceIcons(); places = placeSets; Enumeration eSets = placeSets.keys(); while(eSets.hasMoreElements()) { String placeSetName = (String)eSets.nextElement(); Hashtable placeSet = (Hashtable)placeSets.get(placeSetName); Enumeration ePlaces = placeSet.elements(); while(ePlaces.hasMoreElements()) { // we don't want anything to do with stuff // that we couldn't possibly display on this map // it will just slow us down. PlaceBacking place = (PlaceBacking)ePlaces.nextElement(); if(placeOnMap(place)) { MapIcon placeIcon = new MapIcon(this, place, iconTextBg, zoom); //zoomValues[zoomIndex]); placeIcons.addElement(placeIcon); } } } } protected boolean placeOnMap(PlaceBacking place) { return mapData.containsCoordinate(new TwoDCoordinate(place.lat, place.lon)); } public MapBacking getMapData() { return mapData; } /** * Gets the origin latitude of the currently onscreen map. * Note the origin is defined to be the lower left corner */ public double getOriginLat() { return mapData.getOriginLat(); } /** * Gets the origin longitude of the currently onscreen map. * Note the origin is defined to be the lower left corner */ public double getOriginLon() { return mapData.getOriginLon(); } public double getPixelsPerLat() { int height = holder.getSize().y; double pixelsPerLat = (double)height / (mapData.getMaxLat() - mapData.getOriginLat()); return pixelsPerLat; } public double getPixelsPerLon() { int width = holder.getSize().x; double pixelsPerLon = (double)width / (mapData.getMaxLon() - mapData.getOriginLon()); return pixelsPerLon; } public double pixelsToLatitude(int pixelsY) { return getOriginLat() + ((holder.getBounds().height - (double)pixelsY) / getPixelsPerLat()); } public double pixelsToLongitude(int pixelsX) { return getOriginLon() + ((double)pixelsX / getPixelsPerLon()); } public int latitudeToPixels(double lat) { if(Double.isNaN(lat)) { throw new IllegalArgumentException("You passed NaN to me. SWT allows pixels to be drawn at NaN, but it silently breaks all kinds of stuff, so we don't allow it"); } if(mapData == null) return -1; if(mapData instanceof BitmapMapBacking) { return ((BitmapMapBacking)mapData).latitudeToPixels(lat); } else { double tmp = holder.getBounds().height - ((lat - mapData.getOriginLat()) * getPixelsPerLat()); return (int)tmp; } } public int longitudeToPixels(double lon) { if(Double.isNaN(lon)) { throw new IllegalArgumentException("You passed NaN to me. SWT allows pixels to be drawn at NaN, but it silently breaks all kinds of stuff, so we don't allow it"); } if(mapData == null) return -1; if(mapData instanceof BitmapMapBacking) { return ((BitmapMapBacking)mapData).longitudeToPixels(lon); } else { double tmp = (lon - mapData.getOriginLon()) * getPixelsPerLon(); return (int)tmp; } } public Point getPoint(TwoDCoordinate coord) { if (!containsCoordinate(coord)) return null; return new Point(longitudeToPixels(coord.getLongitude()), latitudeToPixels(coord.getLatitude())); } public boolean containsCoordinate(TwoDCoordinate coord) { return mapData.containsCoordinate(coord); } public void doscroll(int x, int y) { // jws - this has a bug in it somewhere - doesnt work at zoom != 1.0 if (isDisposed()) return; Rectangle clientArea = getClientArea(); Rectangle bounds = holder.getBounds(); /* adjust the coordinate system so that the bounds * start at (0,0) */ Rectangle center = new Rectangle(clientArea.x, clientArea.y, clientArea.width, clientArea.height); center.x = -bounds.x; center.y = -bounds.y; bounds.x = bounds.y = 0; /* give a little margin around the sides */ center.x += SHIFT; center.y += SHIFT; center.width -= 2 * SHIFT; center.height -= 2 * SHIFT; if (center.contains(x, y)) return; Point origin = new Point(0, 0); if (x < center.x) { origin.x = x - SHIFT; if (origin.x < 0) origin.x = 0; } else if (x >= center.x + center.width) { origin.x = x + SHIFT - clientArea.width; if (origin.x >= bounds.width- clientArea.width) origin.x = bounds.width- clientArea.width; } else origin.x = clientArea.x; if (y < center.y) { origin.y = y - SHIFT; if (origin.y < 0) origin.y = 0; } else if (y >= center.y + center.height) { origin.y = y + SHIFT - clientArea.height; if (origin.x >= bounds.width- clientArea.height) origin.x = bounds.width- clientArea.height; } else origin.y = clientArea.y; if (origin.x != clientArea.x || origin.y != clientArea.y) setOrigin(origin); } static final int MARGIN=SHIFT; static final double MAXZOOM=5.0; static final double BEACON_SCALE_FACTOR = 10.0; static final double HYSTERESIS = 1.2; static final boolean usePresetZooms = true; public void dozoom() { // calculate correct centering and zoom // aim 1: keep all reticles on screen (yes) // aim 2: keep seen beacons on screen (yes) // aim 3: allow for motion (not yet) if (isDisposed()) return; Rectangle newArea = null; Rectangle gbounds; int r; Enumeration i = overlays.elements(); while(i.hasMoreElements()) { MapViewOverlay o = (MapViewOverlay)i.nextElement(); Rectangle area = o.getSuggestedArea(); if(newArea == null) newArea = area; if(area == null) continue; else newArea.union(area); } // if nobody has any suggestions then the current view is as good as anywhere if(newArea == null) return; // expand newArea a bit (should temper this with screen size constraints) newArea.width += 2*MARGIN; newArea.height += 2*MARGIN; newArea.x -= MARGIN; newArea.y -= MARGIN; // area taken on screen (starts 0,0) Rectangle clientArea = getClientArea(); // area in window (inc scrolled out bits) Rectangle imagebounds = holder.getBounds(); //System.out.println("clientArea " + clientArea + " bounds " + bounds); double newzoom = ((double) clientArea.width) / ((double) newArea.width) / zoom; //if(newzoom < 1.0 / MAXZOOM) newzoom = 1.0 / MAXZOOM; //if(newzoom > MAXZOOM) newzoom = MAXZOOM; double oldzoom = zoom; //fSystem.out.println("newArea is " + newArea + ", current zoom is " + zoom + ", current area is " + clientArea.width + " new zoom is " + newzoom); if(usePresetZooms) { if(newzoom > zoom * HYSTERESIS) { zoomIn(); newzoom = zoom; } else if(newzoom < zoom / HYSTERESIS) { zoomOut(); newzoom = zoom; } else { newzoom = zoom; } } else { setZoom(newzoom); } // new area covered by window imagebounds = holder.getBounds(); newArea.x = (int)(((double)newArea.x)*newzoom / oldzoom); newArea.y = (int)(((double)newArea.y)*newzoom / oldzoom); newArea.width = (int)(((double)newArea.width)*newzoom / oldzoom); newArea.height = (int)(((double)newArea.height)*newzoom / oldzoom); // determine new origin Point origin = new Point(newArea.x + (newArea.width-clientArea.width)/ 2, newArea.y + (newArea.height-clientArea.height)/2); if(origin.x < 0) origin.x = 0; if(origin.y < 0) origin.y = 0; if(origin.x + clientArea.width > imagebounds.width) origin.x = imagebounds.width - clientArea.width; if(origin.y + clientArea.height > imagebounds.height) origin.y = imagebounds.width - clientArea.height; //if (origin.x != imagebounds.x || origin.y != imagebounds.y) setOrigin(origin); } public void setSize(int width, int height) {//ALM if(tigerMode) {// holder.setSize(width, height);// this.setMapData(mapData);// } super.setSize(width, height); } public void setSize(Point size) { this.setSize(size.x, size.y); } /** * Dumps the current state of the mapview to an image file at the specified path * @param path the path to save the image to * @param imageFlags the image type to save as (see org.eclipse.swt.graphics.ImageLoader) */ public void saveMapAsImage(String path, int imageFlags) throws SWTException { // dodgy hack this.holder.doFullDraw(); Image mapImage = this.holder.getOffscreenDrawable(); ImageLoader il = new ImageLoader(); il.data = new ImageData[] { mapImage.getImageData() }; il.save(path, imageFlags); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -