I am trying to implement a double-touch zoom function in my MapView. An event always fires the first time, but not at subsequent times. Below is my code. I have a feeling that this is due to the fact that the card controller was lost after the first launch of the event.
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() {
I also tried changing the OnTouch method to pass the incoming view to MapView, getting the controller while the event was firing. However, I get the same results when the first event fires, but does not follow.
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; }
Being as basic as possible, I cut out all the code in the OnTouch method and programmed it to just 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; }
This works as expected, showing Toast every time the MapView is touched.
I do not understand why the event will fire properly in this case, but not in my previous implementation.
android event-handling ontouchlistener android-mapview
amandion
source share