Android : Android - get X and Y coordinates of dynamically drawn bitmaps

on Tuesday, February 17, 2015


I am making an Android app with OpenStreetMaps (using osmdroid). On this map, markers are shown (the long/lat and other data for these markers are retrieved from a json file). These markers are OverlayItems and are all shown correctly on the MapView. The X and Y coordinates for these bitmaps are calculated by getting a projection of the mapview and then from the geopoint's X and Y in combination with the bitmaps width/2 and height/2 the bitmap is drawn on his correct location on the worldmap.


Long story short: the X and Y values for the bitmaps are calculated dynamically. My question is, I'm writing code that should occur when the user has clicked (=made a touch event) on a marker-bitmap. For this, I need to check if the event's X and Y is within the bounds of the bitmap. How can I do this?


Unfortunately, there doesn't seem to be an easy bitmap.getX or .getY, which is basically what I need.


This is the code for drawing the bitmaps (happens in the @Override draw()) if it could be of any use:



for(OverlayItem p: waypointMarkers){
IGeoPoint in = p.getPoint();
Point out = new Point();
mapview.getProjection().toPixels(in, out);

Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.marker);

bitmaps.put(bm, p);

canvas.drawBitmap(bm,
out.x - bm.getWidth()/2, //shift the bitmap center
out.y - bm.getHeight()/2, //shift the bitmap center
null);

}


The bitmaps.put(bm, p); is a HashMap with Bitmap, OverlayItem pair. Later in my code, I'm looping through the keys of this HashMap and there is where I want to check for the bitmap's X and Y.



for(Bitmap b: bitmaps.keySet()){
if (x >= xOfBitmap && x < (xOfBitmap + yourBitmap.getWidth())
&& y >= yOfBitmap && y < (yOfBitmap + yourBitmap.getHeight())) {
//found this on StackOverflow, if this is true than user has clicked somewhere on the bitmap
}


Thanks in advance.


0 comments:

Post a Comment