Double-Tap to Zoom in Android Maps

Double-Tap to zoom is a typical feature for mapped interfaces on Android. Sometimes it’s just annoying trying to reverse-pinch to zoom with one hand while holding a device.

This is something that’s not technically difficult to do, but it’s not documented very well.

Here’s how to do it:

  1. Write a class that inherits from MapView, and add a GestureDetector to the child class. Then, override the onTouchEvent(MotionEvent event) method and pass the MotionEvent along to your GestureDetector:
package com.justinschultz.android.controls;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import com.google.android.maps.MapView;

public class MyMapView extends MapView
{
	private GestureDetector gestureDetector;

	public MyMapView(Context context, AttributeSet attrs)
	{
		super(context, attrs);

		gestureDetector = new GestureDetector((OnGestureListener) context);
		gestureDetector.setOnDoubleTapListener((OnDoubleTapListener) context);
	}
        
    @Override
	public boolean onTouchEvent(MotionEvent ev)
	{
		return this.gestureDetector.onTouchEvent(ev);
	}
}
  1. Refer to this child class in your layout XML file, instead of the regular MapView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent">
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
	    android:orientation="vertical" 
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">

		<com.justinschultz.android.controls.MyMapView
	    xmlns:android="http://schemas.android.com/apk/res/android"
	    android:id="@+id/mapview"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:clickable="true"
	    android:apiKey="YOUR_API_KEY" />

	</RelativeLayout>
</LinearLayout>
  1. Implement the OnGestureListener and OnDoubleTapListener interfaces in your MapActivity class, and write some code to zoom-in the map in the onDoubleTap(MotionEvent e) method:
public class Map extends MapActivity 
    implements OnGestureListener, OnDoubleTapListener {
 // More map stuff here, and additional forced overrides from the interfaces
 @Override
 public boolean onDoubleTap(MotionEvent e) {
      // Assuming your MapView is called mapView
     mapView.getController().zoomIn();
     return true;
 }

And that’s it! Your map will now zoom-in when the user double-taps.


About this entry