我想做一个响应双击的mapview并获取点击位置的地点,并将其返回给调用者活动..我在eclipse模拟器中测试我的应用程序,但是当我双击地图时没有任何反应和我的onDoubleTap ()方法永远不会进入
这是我的代码
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;
}
}
什么是错的或什么错过了让我的双击永远不会发射?
答案 0 :(得分:7)
我想出了我的代码中遗漏了什么,我在这里写给任何能够遇到同样问题的人
第一
我必须在初始化gestureDetector对象
后添加此行 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);
}
});
第二: 我必须在onDown方法中返回true,因为每次点击都会调用它,双击取决于它
@Override
public boolean onDown(MotionEvent e) {
return true;
}
这就是您在活动中实现doubleTap监听器所需的全部内容。
答案 1 :(得分:0)
让您的活动仅实施OnGestureListener
并在onCreate()
内执行
gd.setOnDoubleTapListener(new OnDoubleTapListener()
{ //all the override functions of the listener go here //
});
在onCreate()
之后,您想要覆盖onGesturelistener
的所有功能,包括您忘记的那个:
@Override
public boolean onTouchEvent(MotionEvent event)
{
return gd.onTouchEvent(event);//return the double tap events
}