I found a little optimization for the CommonsWare drag and drop's brilliant function.
I was doing some precise meashurements with markers on my map, and I wanted my marker to be exactly on the spot I touched and lifted my finger so the meashurment was exactly precise.
On the original from CommonsWare if you touch "near" the marker, the marker does not go to that exact point, but moves along with your finger's movement.
I needed the marker to appear just below my finger on the ACTION_DOWN, ACTION_MOVE and ACTION_UP.
Here is the code if someone need's it. I have to thank CommonsWare to make this function, it's a really good idea.
This is the part of the code I modified.
My MapView is mapa;
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action=event.getAction();
final int x=(int)event.getX();
final int y=(int)event.getY();
boolean result=false;
if (action==MotionEvent.ACTION_DOWN) {
for (OverlayItem item : mOverlays) {
Point p=new Point(0,0);
mapa.getProjection().toPixels(item.getPoint(), p);
//I maintain the hitTest's bounds so you can still
//press near the marker
if (hitTest(item, marker, x-p.x, y-p.y)) {
result=true;
inDrag=item;
mOverlays.remove(inDrag);
populate();
//Instead of using the DragImageOffSet and DragTouchOffSet
//I use the x and y coordenates from the Point
setDragImagePosition(x, y);
dragImage.setVisibility(View.VISIBLE);
break;
}
}
}
else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
setDragImagePosition(x, y);
result=true;
}
else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
dragImage.setVisibility(View.GONE);
//I get the geopoint without using any OffSet, directly with the
//Point coordenates
GeoPoint pt=mapa.getProjection().fromPixels(x,y);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
orig = inDrag.getMarker(0);
//In my case I had down heading Arrows as markers, so I wanted my
//bounds to be at the center bottom
toDrop.setMarker(boundCenterBottom(orig));
mOverlays.add(toDrop);
populate();
inDrag=null;
result=true;
}
return(result || super.onTouchEvent(event, mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp=
(RelativeLayout.LayoutParams)dragImage.getLayoutParams();
//Instead of using OffSet I use the Point coordenates.
//I want my arrow to appear pointing to the point I am moving, so
//I set the margins as the Point coordenates minus the Height and half the
//width of my arrow.
lp.setMargins(x-(dragImage.getWidth()/2),y-dragImage.getHeight(), 0, 0);
dragImage.setLayoutParams(lp);
}
With this you get your arrow appearing where you press with your, instead of the arrow moving from where it was.