1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | package irdc.ex10_06; import java.util.List; import java.util.Iterator; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.graphics.RectF; import android.graphics.Paint.Style; import android.location.Location; import com.google.android.maps.GeoPoint; import com.google.android.maps.Overlay; import com.google.android.maps.MapView; import com.google.android.maps.Projection; public class GeoPointImageOverlay extends Overlay { private static final int PADDING_X = 10; private static final int PADDING_Y = 4; private static final int TEXT_OFFSET_X = 10; private static final int TEXT_OFFSET_Y = 15; private int drawableID; private List<MapLocation> listMapLocations; private MapLocation selectedLocation; private double geoLatitude,geoLongitude; private Bitmap bubbleIcon, shadowIcon, bubbleOffIcon; private Paint innerPaint, borderPaint, textPaint; private Location mLocation; private List<MapLocation> gp; public GeoPointImageOverlay(GeoPoint gp, int i, List<MapLocation> mapLocations) { this.geoLatitude = gp.getLatitudeE6(); this.geoLongitude = gp.getLongitudeE6(); this.drawableID = i; this.listMapLocations = mapLocations; } public void draw(Canvas canvas, MapView mapView, boolean shadow) { // draw(canvas, mapView, shadow); // drawInfoWindow(canvas, mapView, shadow); // drawMapLocations(canvas, mapView, shadow); } @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { shadowIcon = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.shadow); Projection projection = mapView.getProjection(); if (shadow ) {shadowIcon = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.shadow); GeoPoint gp = new GeoPoint((int)this.geoLatitude,(int)this.geoLongitude); /* 將Location轉成Point物件 */ Point point = new Point(); projection.toPixels(gp, point); /* 設定筆刷 */ Paint paint = new Paint(); paint.setAntiAlias(true); /* 繪製Overlay*/ //canvas.drawOval(oval, paint); Bitmap bm = BitmapFactory.decodeResource(mapView.getResources(), R.drawable.bubble); canvas.drawBitmap(bm, point.x , point.y , paint); canvas.drawBitmap(shadowIcon, point.x + shadowIcon.getWidth()/3 , point.y + shadowIcon.getHeight()/2 ,paint ); } return super.draw(canvas, mapView, shadow, when); } public void updateLocation(Location location) { this.mLocation = location; } public Location getLocation() { return mLocation; } public boolean onTap(GeoPoint point, MapView mapView) { // Store whether prior popup was displayed so we can call invalidate() & remove it if necessary boolean isRemovePriorPopup = selectedLocation != null; // Test whether a new popup should be displayed selectedLocation = getHitMapLocation(mapView, point); if (isRemovePriorPopup || selectedLocation != null) { mapView.invalidate(); } // Lastly return true if we handled this onTap() return selectedLocation != null; } /* Test whether an information balloon should be displayed or a prior balloon hidden */ private MapLocation getHitMapLocation(MapView mapView, GeoPoint tapPoint) { MapLocation hitMapLocation = null; // Track which MapLocation was hit... if any RectF hitTestRect = new RectF(); Point screenCoords = new Point(); Iterator<MapLocation> iterator = listMapLocations.iterator(); while(iterator.hasNext()) { MapLocation testLocation = iterator.next(); // Translate the MapLocation's lat/long coordinates to screen coordinates mapView.getProjection().toPixels(testLocation.getPoint(), screenCoords); // Create a 'hit' testing rectangle w/size and coordinates of our icon // Set the 'hit' testing rectangle with the size and coordinates of our on screen icon hitTestRect.set(-bubbleIcon.getWidth()/2, -bubbleIcon.getHeight(), bubbleIcon.getWidth()/2, 0); hitTestRect.offset(screenCoords.x, screenCoords.y); // Finally test for a match between our 'hit' Rectangle and the location clicked by the user mapView.getProjection().toPixels(tapPoint, screenCoords); if (hitTestRect.contains(screenCoords.x, screenCoords.y)) { hitMapLocation = testLocation; break; } } // Lastly clear the new mouse selection as it has now been processed tapPoint = null; return hitMapLocation; } private int getWidthText() { return (int)textPaint.measureText(selectedLocation.getName()); } private int getHeightText() { return (int)textPaint.descent()-(int)textPaint.ascent(); } } |
Direct link: https://paste.plurk.com/show/296979