双击/点击地图缩放(Android)

时间:2010-12-15 20:17:48

标签: android google-maps

  

可能重复:
  Double Tap -> Zoom on Android MapView?

我有Activity扩展MapActivity。我使用谷歌地图 我需要通过双击或双击放大。

有人可以帮我这个吗?

我已经看过下面的内容了,但它们并不是我想要的。

Double Tap -> Zoom on Android MapView?

Fling gesture detection on grid layout

我意识到onTouchListener只被调用一次,为什么会这样?

// set Gesture
detector = new GestureDetector(new GestureReactor());
mapView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("Inside onTouch");
        return detector.onTouchEvent(event);
    }
});

我有私人课程:

private class GestureReactor extends SimpleOnGestureListener {
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        System.out.println("inside onDouble");
        controller.zoomInFixing((int) e.getX(), (int) e.getY());
        return super.onDoubleTap(e);
    }
}

private GestureDetector detector;

6 个答案:

答案 0 :(得分:3)

由于您需要复制粘贴答案,请查看here

编辑:

在我的public class MainMap extends MapActivity我有一个名为private MyMapView mv;

的字段

然后我创建了一个扩展MapView的类,如下所示: public class MyMapView extends MapView

因此,您只需创建一个extends MapView的新课程,复制粘贴在此处链接中找到的代码,然后在您的class <。活动中使用新的extends MapActivity。 / p>

答案 1 :(得分:3)

答案 2 :(得分:3)

我喜欢这种使用自定义视图扩展MapView并在整个应用程序中使用它的解决方案。 MapView具有拦截触摸手势的功能,您可以使用它来执行双击伏都教。如果您的应用程序中有多个地图,这是保持DRY的好方法。

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import com.google.android.maps.MapView;

public class MyMapView extends MapView {
    private long lastTouchTime = -1;

    public MyMapView(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);
    }
}

来自this guy

答案 3 :(得分:3)

我和bbodenmiller有同样的问题,一切正常,直到我不得不施展到新制作的课程。 mapView =(MyMapView)findViewById(R.id.mapview);会一直抛出异常。

解决方案是更改我们显示自定义MapView的XML布局。当您使用vanilla MapView时,您的XML布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="Hidden for obvious reasons"
/>

问题在于“com.google.android.maps.MapView”将视图定义为MapView。您需要使用自定义的MapView来定义它。最终看起来更像这样。

<?xml version="1.0" encoding="utf-8"?>
<jocquej.PackageName.MyMapView
    ...
/>

答案 4 :(得分:3)

我一直在寻找答案/示例,但发现无处可用的代码。

最后,这是适合我的代码:

<强> 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>

不要忘记在此处用android:apiKey替换apiKey值。

答案 5 :(得分:0)

我无法让http://nocivus.posterous.com/double-clicktap-detection-on-a像许多其他人一样工作,因为它不允许我施放mapView = (MyMapView) findViewById(R.id.mapview);被称为我得到的最佳解决方案是基于{{3}的答案}:

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;
    }
};
相关问题