java 双击:放大 Android MapView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2691235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double tap: zoom on Android MapView?
提问by poeschlorn
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?
你能给我一个提示吗?
采纳答案by reflog
Implement GestureListener to receive doubleTap events.
实现 GestureListener 以接收 doubleTap 事件。
see: Fling gesture detection on grid layout
请参阅:网格布局上的甩动手势检测
回答by azizbekian
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
我的地图活动.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
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:apiKeyvalue with your apiKey.
不要忘记在此处android:apiKey用您的apiKey.
回答by Bachi
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)
(所有那些在这个问题上绊倒的人的迟到答案)
回答by Igor Deruga
An old post, I know, but this solution works too: double click/tap map zoom (Android)
我知道一个旧帖子,但这个解决方案也有效:双击/点击地图缩放(Android)
I think he fixed his errors in his own post, so just use the question part, not the answers (they are horrible :))
我认为他在他自己的帖子中修正了他的错误,所以只使用问题部分,而不是答案(它们很糟糕:))
回答by Sudhir
Use the following code when you want to zoom in and zoom out
要放大和缩小时使用以下代码
- zoom in double click on top 50% of mapview
- Zoom out double click on bottom 50% of mapview
- 放大双击地图视图的前 50%
- 缩小双击地图视图底部 50%
@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);
}
回答by mattyU
In case somebody is looking for the answer for the new GoogleMap (Since Google-Play-service:9+)
如果有人正在寻找新 GoogleMap 的答案(自 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;

