2

Possible Duplicate:
Double Tap -> Zoom on Android MapView?

I am newbie Android developer. I am developing an application in which I am using GoogleMaps. I have to implement double tap zoom in GoogleMaps. When I double click on the map it should zoom in. Kindly if it is possible provide a sample code.
I have written the following code but I dont know what to add more.

public class Maps extends MapActivity implements OnGestureListener, OnDoubleTapListener {

    private GestureDetector detector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // MapViewer mapViewer = new MapViewer(null, null);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        detector = new GestureDetector(this, this);
        mapView.setBuiltInZoomControls(true);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public boolean onDoubleTap(MotionEvent e) {
        mapView.getController().zoomIn();
        return false;
    }

    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    public boolean onDown(MotionEvent e) {
        return false;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    public void onLongPress(MotionEvent e) {
    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    public void onShowPress(MotionEvent e) {
    }

    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}
Community
  • 1
  • 1
zeeshan
  • 21
  • 1
  • 3

4 Answers4

4

Based on the information provided here and double click/tap map zoom (Android) the best solution I got was:

myLocationOverlay = new MyLocationOverlay(this, mapView);

MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay() {
    private long systemTime = System.currentTimeMillis();

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if ((System.currentTimeMillis() - systemTime) < ViewConfiguration.getDoubleTapTimeout()) {
                mapController.zoomInFixing((int) event.getX(), (int) event.getY());
            }
            break;
        case MotionEvent.ACTION_UP:
            systemTime = System.currentTimeMillis();
            break;
        }

        return false;
    }
};

case MotionEvent.ACTION_UP was added as a result of how https://developer.android.com/reference/android/view/ViewConfiguration.html#getDoubleTapTimeout() says getDoubleTapTimeout() is to be calculated. Additionally I changed the zoom function as this one is the least jumpy and functions identical to the Maps app.

Community
  • 1
  • 1
bbodenmiller
  • 3,101
  • 5
  • 34
  • 50
  • This seems to be the way to go, same kind of implementation as first answer, but I like the thoroughness and zoom behavior of this a bit more. – geerlingguy Jun 16 '12 at 02:42
  • It's worth noting that I have since discovered that when the user is panning it will often times incorrectly zoom in as well. I haven't had time to fix this yet but will post when I get it done. – bbodenmiller Jun 16 '12 at 08:59
2
    @Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if ((System.currentTimeMillis() - systemTime) < 200) {
            mapView.getController().zoomIn();
        }
        systemTime = System.currentTimeMillis();
        break;
    }

    return false;
}

This will simulate a zoom-in. Implements in your MapOverlay (ItemizedOverlay)

Freddroid
  • 2,449
  • 22
  • 23
0

To make it zoom into the tapped location, just add:

getController().animateTo(getProjection().fromPixels((int) ev.getX(), (int) ev.getY()));
antyrat
  • 27,479
  • 9
  • 75
  • 76
Lukas Olsen
  • 5,294
  • 7
  • 22
  • 28
0

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 < 250) {

        // 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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • This looks like a nice, lightweight way to detect double-taps. Understand that 1) a MotionEvent knows when it was created, so you can write: `long thisTime = event.getEventTime();`. 2) If a user is rapidly panning this will mistake two fast fling gestures for a double-tap (you should check how far both tap events move). 3) Please do not post the exact same answer in multiple question, if the questions are identical leave a comment or flag it with a link to the duplicate question. – Sam Sep 28 '12 at 20:26
  • 1) Got it. 2) Yea, you are right here too. 3) Will keep in mind for the next time. – azizbekian Sep 28 '12 at 20:42