经过一些工作后,我的路线应用程序正常工作。 我唯一想要添加的是双击放大功能,但我不知道如何。
你能给我一个提示吗?
答案 0 :(得分:6)
我一直在寻找答案/示例,但发现无处可用的代码。
最后,这里是为我工作的代码:
<强> 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 < 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>
请勿忘记将此android:apiKey
值替换为apiKey
。
答案 1 :(得分:1)
实施GestureListener以接收doubleTap事件。
答案 2 :(得分:1)
答案 3 :(得分:1)
我知道一篇旧帖子,但这个解决方案也有效:double click/tap map zoom (Android)
我认为他在自己的帖子中修正了他的错误,所以只使用问题部分,而不是答案(它们太可怕了:))
答案 4 :(得分:0)
如果要放大和缩小
,请使用以下代码
@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);
}
答案 5 :(得分:0)
如果有人正在寻找新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;