OnTouch in MapView only launches for the first time

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() { // 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; } } 

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.

+6
android event-handling ontouchlistener android-mapview
source share
6 answers

If you use this method "mapView.setBuiltInZoomControls(true);" , then your touch works right away.

Delete this line and make sure it will work.

In some cases, if you want BuiltInZoomControls, then you can use the OnTouch Overlay method, as shown 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; } } 
+15
source share

I had the same problem - I could not drag and scroll the map because it only received ACTION_DOWN events. It can be solved by adding android:clickable="true" in MapView or by calling mapView.setClickable(true) .

+1
source share

Subclass MapView and override dispatchTouchEvent as shown below and use the subclass instead.

 public class MyMap extends MapView{ @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (mOnMovedListener!= null) { mOnMovedListener.onMove(ev); } return super.dispatchTouchEvent(ev); } private OnMovedListener mOnMovedListener; public void setOnMovedListener(OnMovedListener mOnMovedListener) { this.mOnMovedListener= mOnMovedListener; } } 

register for the listener, like any other!

+1
source share

How about using GestureDetector.OnDoubleTapListener instead of OnTouchListener ?

0
source share

you should at least put

 lasttime=event.getEventTime(); 

under

 if (event.getAction() == MotionEvent.ACTION_DOWN) brakes 

while onTouch detects the onTouch event of your click. Therefore, at any time when you click, it is called 2 times

0
source share

Implement the Touch event on the map. It will work!

  // The onTouch event of the Map itself does not fire! // We must implement it on the mapView!!! mapView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // Your code and remember to return true! return (true); } }); 
-one
source share

All Articles