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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 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 com.google.android.maps.GeoPoint; import com.google.android.maps.Overlay; import com.google.android.maps.MapView; 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; public GeoPointImageOverlay(List<MapLocation> mapLocations) { listMapLocations = mapLocations; } public void draw(Canvas canvas, MapView mapView, boolean shadow) { drawMapLocations(canvas, mapView, shadow); drawInfoWindow(canvas, mapView, shadow); } 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(); } private void drawMapLocations(Canvas canvas, MapView mapView, boolean shadow) { bubbleIcon = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.bubble); shadowIcon = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.shadow); // bubbleOffIcon = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.bubble_off); Iterator<MapLocation> iterator = listMapLocations.iterator(); Point myPoint = new Point(); while(iterator.hasNext()) { MapLocation location = iterator.next(); mapView.getProjection().toPixels(location.getPoint(), myPoint); if (shadow) { // Only offset the shadow in the y-axis as the shadow is angled so the base is at x=0; canvas.drawBitmap(shadowIcon, myPoint.x, myPoint.y - shadowIcon.getHeight(), null); } else { // canvas.drawBitmap(bubbleOffIcon, myPoint.x - bubbleOffIcon.getWidth()/2, myPoint.y - bubbleOffIcon.getHeight(), null); } } } private void drawInfoWindow(Canvas canvas, MapView mapView, boolean shadow) { if (selectedLocation != null) { if (shadow) { } else { // First determine the screen coordinates of the selected MapLocation Point selDestinationOffset = new Point(); mapView.getProjection().toPixels(selectedLocation.getPoint(), selDestinationOffset); canvas.drawBitmap(bubbleIcon, selDestinationOffset.x - bubbleIcon.getWidth()/2, selDestinationOffset.y - bubbleIcon.getHeight(), null); if (innerPaint == null) { innerPaint = new Paint(); innerPaint.setARGB(225, 75, 75, 75); // Gray innerPaint.setAntiAlias(true); } if (borderPaint == null) { borderPaint = new Paint(); borderPaint.setARGB(255, 255, 255, 255); borderPaint.setAntiAlias(true); borderPaint.setStyle(Style.STROKE); borderPaint.setStrokeWidth(2); } if (textPaint == null) { textPaint = new Paint(); textPaint.setARGB(255, 255, 255, 255); textPaint.setAntiAlias(true); } // Setup the info window with the right size & location int INFO_WINDOW_WIDTH = getWidthText() + (PADDING_X * 2); int INFO_WINDOW_HEIGHT = getHeightText() + (PADDING_Y * 2); RectF infoWindowRect = new RectF(0, 0, INFO_WINDOW_WIDTH, INFO_WINDOW_HEIGHT); int infoWindowOffsetX = selDestinationOffset.x - INFO_WINDOW_WIDTH/2; int infoWindowOffsetY = selDestinationOffset.y - INFO_WINDOW_HEIGHT - bubbleIcon.getHeight(); infoWindowRect.offset(infoWindowOffsetX, infoWindowOffsetY); // Draw inner info window canvas.drawRoundRect(infoWindowRect, 5, 5, innerPaint); // Draw border for info window canvas.drawRoundRect(infoWindowRect, 5, 5, borderPaint); // Draw the MapLocation's name canvas.drawText(selectedLocation.getName(), infoWindowOffsetX + TEXT_OFFSET_X, infoWindowOffsetY + TEXT_OFFSET_Y, textPaint); } } } } |
Direct link: https://paste.plurk.com/show/296368