Double tap: zoom on Android MapView?
Asked Answered
H

6

2

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?

Herbivorous answered 22/4, 2010 at 13:34 Comment(0)
R
1

Implement GestureListener to receive doubleTap events.

see: Fling gesture detection on grid layout

Raulrausch answered 22/4, 2010 at 13:45 Comment(1)
there is not double tap in that post why does everyone redirect to that particular thread :|Onomastic
R
6

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.

Riocard answered 28/9, 2012 at 19:49 Comment(1)
The source of the onInterceptTouchEvent handler seems to be this blog post. I improved the code of Andrianik by setting ViewConfiguration.getDoubleTapTimeout().Pablo
R
1

Implement GestureListener to receive doubleTap events.

see: Fling gesture detection on grid layout

Raulrausch answered 22/4, 2010 at 13:45 Comment(1)
there is not double tap in that post why does everyone redirect to that particular thread :|Onomastic
H
1

Here is a nice and down-to-the-point implementation:

Double Tap Zoom in GoogleMaps Activity

(a late answer for all those that stuble over this question)

Heavenly answered 30/3, 2012 at 19:10 Comment(0)
D
1

An old post, I know, but this solution works too: double click/tap map zoom (Android)

I think he fixed his errors in his own post, so just use the question part, not the answers (they are horrible :))

Droll answered 4/5, 2012 at 16:3 Comment(0)
U
0

Use the following code when you want to zoom in and zoom out

  1. zoom in double click on top 50% of mapview
  2. Zoom out double click on bottom 50% of mapview

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      long thisTime = System.currentTimeMillis();

DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) activity.getSystemService(activity.getApplicationContext().WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels; 

if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
        // Double tap
         if((height/2)>ev.getY()){
// Zoom IN
            this.getController().zoomInFixing((int) ev.getX(), (int) ev.getY());
        }else{
            this.getController().zoomOutFixing((int) ev.getX(), (int) ev.getY());
//Zoom Out
        }
        lastTouchTime = -1;
      } else {
        // Too slow 
        lastTouchTime = thisTime;
      }
    }
    return super.onInterceptTouchEvent(ev);
  }
Urias answered 20/5, 2013 at 3:54 Comment(0)
S
0

In case somebody is looking for the answer for the new GoogleMap (Since Google-Play-service:9+)

case MotionEvent.ACTION_DOWN:
                x1 = m.getX();
                if ((System.currentTimeMillis() - systemTime) < 200) {
                    mGoogleMap.animateCamera(CameraUpdateFactory.zoomIn());
                }
                systemTime = System.currentTimeMillis();
                break;
case MotionEvent.ACTION_UP:
                systemTime = System.currentTimeMillis();
                ...
                break;
Scram answered 8/1, 2017 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.