views:

127

answers:

1

I just cant get this working. I tried using the code below with onTouchEventand and it doesn't work. If i return true at the end of the method, i get the toast with the coordinates but can't move a map, and if i return false, i can move a map but cant display a toast after the user clicks on a map. If i get it right, the other onTap method is used only for clicking on a overlays. Has anybody figured this thiing out?

        public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {

       //super.onTouchEvent(arg0);


       int akcija = arg0.getAction(); 

        if(akcija == MotionEvent.ACTION_UP){
            if(!premik) {
                Projection proj = mapView.getProjection();
                GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY()); 
                String sirina=Double.toString(loc.getLongitudeE6()/1000000);
                String dolzina=Double.toString(loc.getLatitudeE6()/1000000);

                 Toast toast = Toast.makeText(getApplicationContext(), "Širina: "+sirina+" Dolzina: "+dolzina, Toast.LENGTH_LONG);
                toast.show();
            }
        }     

        else if (akcija == MotionEvent.ACTION_DOWN){

            premik= false;

        }
        else if (akcija== MotionEvent.ACTION_MOVE){             
            premik = true;
        }


        return false;
        //return super.onTouchEvent(arg0);
      }
A: 

use dispatchTouchEvent() method. it works. why because the MapActivity inherits the dispatchTouch Event not OnTouchEvent from Activity Class. check this documentation

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int actionType = ev.getAction();
    switch (actionType) {
    case MotionEvent.ACTION_UP:
         if(!premik) {
            Projection proj = mapView.getProjection();
            GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY()); 
            String sirina=Double.toString(loc.getLongitudeE6()/1000000);
            String dolzina=Double.toString(loc.getLatitudeE6()/1000000);

             Toast toast = Toast.makeText(getApplicationContext(), "Širina: "+sirina+" Dolzina: "+dolzina, Toast.LENGTH_LONG);
            toast.show();
        }

    }

    return super.dispatchTouchEvent(ev);
}
Praveen Chandrasekaran
It works in emulator, but fails on my Hero. I guess it's just another hero specific bug :(
DixieFlatline
dispatchTouch event is not detected on my Hero.
DixieFlatline
its by a firmwaret version problem of hero thats API Level is 3.but dispatchtouch Event methods in on API Level 4. so it could not work.
Praveen Chandrasekaran
It's there since API level 1 ==> http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)My friend confirmed that it works on his Samsung spica(1.5 also).
DixieFlatline