問題描述
我正在嘗試在我的 MapView 中實現雙擊縮放功能.該事件總是第一次觸發,但以后不會觸發.下面是我的代碼.我感覺這與第一次觸發事件后地圖控制器丟失有關.
I'm trying to implement a double-tap zoom like function in my MapView. The event always fires the first time, but never subsequent times. Below is my code. I have a feeling it has something to do with the map controller getting lost after the first time the event is fired.
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class mainmap extends MapActivity implements OnTouchListener{
long lasttime = -1;
MapController mapc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapc = mapView.getController();
mapView.setOnTouchListener(this);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if(event.getEventTime()-lasttime<2000){
mapc.zoomInFixing((int)event.getX(),(int)event.getY());
}
}
lasttime=event.getEventTime();
return true;
}
}
我還嘗試編輯 OnTouch 方法以將傳入的 View 轉換為 MapView,在觸發事件時獲取控制器.但是,在觸發第一個事件而不是后續事件的情況下,我得到了相同的結果.
I have also tried editing the OnTouch method to cast the incoming View to a MapView, getting the controller while the event is fired. However, I get the same results where the first event is fired but not subsequent ones.
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if(event.getEventTime()-lasttime<2000){
((MapView)v).getController().zoomInFixing((int)event.getX(), (int)event.getY());
}
}
lasttime=event.getEventTime();
return true;
}
為了盡可能簡單,我刪除了 OnTouch 方法中的所有代碼,并將其編程為簡單地顯示一條小的 toast 消息.
Being as basic as possible, I cut out all of the code in the OnTouch method and programmed it to simply display a small toast message.
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(this,"Down!",Toast.LENGTH_SHORT).show();
}
return true;
}
這按預期工作,每次觸摸 MapView 時都會顯示 Toast.
This works as expected, displaying the Toast each time the MapView is touched.
我不明白為什么在這種情況下事件會正確觸發,但在我之前的實現中卻不會.
I don't understand why the event will fire properly in this case but not in my previous implementation.
推薦答案
如果你使用這個方法 "mapView.setBuiltInZoomControls(true);"
那么你的 touch
是立即工作.
If you are using this method "mapView.setBuiltInZoomControls(true);"
then your touch
is working at once .
請刪除該行并檢查我確定它會起作用..
Please remove that that line and check I am sure it will work..
在某些情況下,如果您想要 BuiltInZoomControls,那么您可以使用 OnTouch 的 Overlay 方法,如下所示..
In some case if you want BuiltInZoomControls then you can you OnTouch method of Overlay like as below..
public class MapOverlay extends Overlay {
public MapOverlay(Context ctx) {super(ctx);}
@Override
protected void draw(Canvas c, MapView osmv, boolean shadow) { }
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
//Write yout touch code here..
return false;
}
}
這篇關于MapView 中的 OnTouch 僅在第一次觸發的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!