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:

[java]
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);
}
}
[/java]

  1. Refer to this child class in your layout XML file, instead of the regular MapView:

[xml]
<?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>
[/xml]

  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:

[java]
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;
}
[/java]
And that’s it! Your map will now zoom-in when the user double-taps.


Posted

in

by

Comments

One response to “Double-Tap to Zoom in Android Maps”

  1. Daniele Avatar
    Daniele

    It didn’t work for me, I had to use this code in the onTouchEvent of the custom MapView:

    if (this.gestureDetector.onTouchEvent(ev))
    return true;
    else
    return super.onTouchEvent(ev);

Leave a Reply

Your email address will not be published. Required fields are marked *