try this:
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
public class MyLongpressMapView extends MapView {
public interface OnLongpressListener {
public void onLongpress(MapView view, GeoPoint longpressLocation);
}
/**
* Time in ms before the OnLongpressListener is triggered.
*/
static final int LONGPRESS_THRESHOLD = 500;
/**
* Keep a record of the center of the map, to know if the map
* has been panned.
*/
private GeoPoint lastMapCenter;
private Timer longpressTimer =
new Timer();
private MyLongpressMapView.OnLongpressListener longpressListener;
private float offset;
public MyLongpressMapView(Context context, String apiKey) {
super(context, apiKey);
}
public MyLongpressMapView(Context context, AttributeSet attrs) {
super(context, attrs);
this.offset =
context.getResources().getDimension(R.dimen.tabbar_height);
}
public MyLongpressMapView(Context context, AttributeSet attrs, int
defStyle) {
super(context, attrs, defStyle);
}
public void
setOnLongpressListener(MyLongpressMapView.OnLongpressListener listener) {
this.longpressListener = listener;
}
/**
* This method is called every time user touches the map,
* drags a finger on the map, or removes finger from the map.
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
handleLongpress(event);
return super.onTouchEvent(event);
}
/**
* This method takes MotionEvents and decides whether or not
* a longpress has been detected. This is the meat of the
* OnLongpressListener.
* The Timer class executes a TimerTask after a given time,
* and we start the timer when a finger touches the screen.
* We then listen for map movements or the finger being
* removed from the screen. If any of these events occur
* before the TimerTask is executed, it gets cancelled. Else
* the listener is fired.
*
* @param event
*/
private void handleLongpress(final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
this.longpressTimer = new Timer();
this.longpressTimer.schedule(new TimerTask() {
@Override
public void run() {
final GeoPoint longpressLocation = getProjection()
.fromPixels((int) event.getX(), (int)
(event.getY() - MyLongpressMapView.this.offset));
if (MyLongpressMapView.this.longpressListener != null) {
MyLongpressMapView.this.longpressListener.onLongpress(MyLongpressMapView.this,
longpressLocation);
}
}
},
LONGPRESS_THRESHOLD);
this.lastMapCenter = getMapCenter();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!getMapCenter().equals(this.lastMapCenter)) {
this.longpressTimer.cancel();
}
this.lastMapCenter = getMapCenter();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
this.longpressTimer.cancel();
}
if (event.getPointerCount() > 1) {
this.longpressTimer.cancel();
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en