双击 GoogleMaps 活动中的“缩放”

发布于 2024-09-08 12:05:23 字数 1736 浏览 4 评论 0原文

可能的重复:
双击 ->缩放 Android MapView?

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

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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想挽留 2024-09-15 12:05:23

根据此处提供的信息和双击/点击地图缩放 (Android) 我得到的最佳解决方案是:

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 是由于 https://developer.android.com/reference/android/view/ViewConfiguration.html#getDoubleTapTimeout()getDoubleTapTimeout( ) 需要计算。此外,我还更改了缩放功能,因为这是最不跳跃的功能,并且功能与地图应用程序相同。

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.

永言不败 2024-09-15 12:05:23
    @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;
}

这将模拟放大。在您的 MapOverlay (ItemizedOverlay) 中实现

    @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)

2024-09-15 12:05:23

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

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

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

getController().animateTo(getProjection().fromPixels((int) ev.getX(), (int) ev.getY()));
左秋 2024-09-15 12:05:23

我也一直在寻找答案/示例,但找不到有效的代码。

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

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);
  }
}

ma​​in.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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文