Issue
I wanna make a mapview that responds to double click and get the geopoint of the clicked place and return it to the caller activity .. I test my app in eclipse emulator, but when I double click the map nothing happens and my onDoubleTap() method never enters
here is my code
public class MYMapActivity extends MapActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
GestureDetector gestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mymapview);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
gestureDetector = new GestureDetector(getApplicationContext(), this);
gestureDetector.setOnDoubleTapListener(this);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent mev) {
MapView mapView = (MapView) findViewById(R.id.mapview);
Projection p = mapView.getProjection();
GeoPoint geoPoint = p.fromPixels((int) mev.getX(), (int) mev.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();
Intent resultIntent = new Intent();
resultIntent.putExtra("latitude", latitude);
resultIntent.putExtra("longitude", longitude);
setResult(Activity.RESULT_OK, resultIntent);
finish();
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
whats wrong or whats missed that makes my double tap never fires ??
Solution
I figured out whats missing on my code and I write it here for any one who can meet the same problem
first:
I must add this line after the initializing of gestureDetector object
gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
second: I must return true in onDown method since it is called with every click and double tap depends on it
@Override
public boolean onDown(MotionEvent e) {
return true;
}
thats all what you need to implement doubleTap listener in your activity.
Answered By - Dorgham
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.