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.
You could have a look at OSMBonusPack Marker .
As it implements the touch event detection, looking at its source code (hitTest method) will give you a working example.
And maybe you can just use it, if it matches your need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.