Double tap: zoom on Android MapView?

poeschlorn picture poeschlorn · Apr 22, 2010 · Viewed 10.6k times · Source

After a little bit of work my route application works fine. The only thing I just want to add is a double tap zoom in function, but I don't know how.

Could you give me a hint?

Answer

azizbekian picture azizbekian · Sep 28, 2012

I've also been searching for an answer/example, but found nowhere working code.

Finally, here's the code that's working for me:

MyMapActivity.java

public class MyMapActivity extends MapActivity
                           implements OnGestureListener, OnDoubleTapListener {

private MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mapView = (MapView)findViewById(R.id.mapView);
}

@Override
public boolean onDoubleTap(MotionEvent e) {
    int x = (int)e.getX(), y = (int)e.getY();;  
    Projection p = mapView.getProjection();  
    mapView.getController().animateTo(p.fromPixels(x, y));
    mapView.getController().zoomIn();  
    return true; 
}

// Here will be some autogenerated methods too

OnDoubleTap.java

public class OnDoubleTap extends MapView {

  private long lastTouchTime = -1;

  public OnDoubleTap(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      long thisTime = System.currentTimeMillis();
      if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
        // Double tap
        this.getController().zoomInFixing((int) ev.getX(), (int) ev.getY());
        lastTouchTime = -1;
      } else {
        // Too slow 
        lastTouchTime = thisTime;
      }
    }
    return super.onInterceptTouchEvent(ev);
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <azizbekyan.andranik.map.OnDoubleTap
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="YOUR_API_KEY" />        
</LinearLayout>

Don't forget to replace here the android:apiKey value with your apiKey.