Android 双击“放大谷歌地图”活动

Android 双击“放大谷歌地图”活动,android,google-maps,double-click,zooming,Android,Google Maps,Double Click,Zooming,可能重复: 我是新的Android开发者。我正在开发一个使用谷歌地图的应用程序。我必须在谷歌地图中实现双击缩放。当我双击地图时,它应该放大。如果可能,请提供示例代码。 我已经写了下面的代码,但我不知道还要添加什么 public class Maps extends MapActivity implements OnGestureListener, OnDoubleTapListener { private GestureDetector detector; @Overrid

可能重复:

我是新的Android开发者。我正在开发一个使用谷歌地图的应用程序。我必须在谷歌地图中实现双击缩放。当我双击地图时,它应该放大。如果可能,请提供示例代码。
我已经写了下面的代码,但我不知道还要添加什么

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;
    }
}
@覆盖
公共布尔onTouchEvent(MotionEvent事件,MapView MapView){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
如果((System.currentTimeMillis()-systemTime)<200){
mapView.getController().zoomIn();
}
systemTime=System.currentTimeMillis();
打破
}
返回false;
}

这将模拟放大。在MapOverlay中实现(ItemizeOverlay)

要将其放大到点击的位置,只需添加:

getController().animateTo(getProjection().fromPixels((int) ev.getX(), (int) ev.getY()));

根据这里提供的信息,我得到的最佳解决方案是:

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;
    }
};
myLocationOverlay=新建myLocationOverlay(此,地图视图);
MyItemizedOverlay itemizedOverlay=新建MyItemizedOverlay(){
private long systemTime=System.currentTimeMillis();
@凌驾
公共布尔onTouchEvent(MotionEvent事件,MapView MapView){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
if((System.currentTimeMillis()-systemTime)

case MotionEvent.ACTION\u UP
是根据如何计算
getDoubleTapTimeout()
而添加的。此外,我还更改了缩放功能,因为这是最不起眼的,功能与“地图”应用程序相同。

我也一直在搜索答案/示例,但没有找到有效的代码

最后,以下是对我有用的代码:

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
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);
  }
}
OnDoubleTap.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
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);
  }
}
公共类OnDoubleTap扩展了MapView{ 私有长lastTouchTime=-1; 公共OnDoubleTap(上下文、属性集属性){ 超级(上下文,attrs); } @凌驾 公共布尔值onInterceptTouchEvent(MotionEvent ev){ if(ev.getAction()==MotionEvent.ACTION\u向下){ long ThistTime=System.currentTimeMillis(); 如果(这次-上次接触时间<250){ //双击 this.getController().zoomInFixing((int)ev.getX(),(int)ev.getY()); lastTouchTime=-1; }否则{ //太慢 lastTouchTime=这次; } } 返回超级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>


不要忘记在这里用您的
apiKey
替换
android:apiKey
值,您可以调用mapView.getController().zoomIn()或zoomOut…调用mapView.getController().zoomIn(),但在哪里呢??你可以编辑我的代码吗?这似乎是一种方式,与第一个答案的实现方式相同,但我更喜欢它的彻底性和缩放行为。值得注意的是,我后来发现,当用户平移时,它也会错误地放大很多倍。我还没来得及修复这个问题,但我会在完成后发布。这看起来是一个很好的、轻量级的检测双击的方法。了解1)MotionEvent知道它是何时创建的,因此您可以编写:
longthistime=event.getEventTime()。2) 如果用户快速平移,这会将两个快速投掷手势误认为是双击(您应该检查两个点击事件移动的距离)。3) 请不要在多个问题中发布完全相同的答案,如果问题相同,请留下评论或标记重复问题的链接。1)明白。2) 是的,你也在这里。3) 下次会记住的。