MapView & onTouch events

Last Updated:

  1. JoshKraker

    JoshKraker Member This Topic's Starter

    Dec 3, 2009
    Likes Received:
    Hello everyone! Hopefully I can get some help here; the google group does not seem very helpful. I'm a beginner to the Android SDK as well as java for that fact (VB.NET background).

    I have a map with some geometry drawn on it by overriding the onDraw event. I'm trying to detect when the user touches on of the shapes and attempting this with the following code

    Code (Text):
    1. public class Map extends MapActivity implements OnTouchListener {
    3.     public MapView _mvMap;
    4.     private Geo.Geometry _oGeo;
    7.     @Override
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.         setContentView(R.layout.mapview);
    12.         _mvMap = (MapView) findViewById(;
    13.         _oGeo = new Geo().new Geometry(_mvMap);
    15.         _mvMap.getOverlays().add(_oGeo);
    16.         _mvMap.setOnTouchListener(this);
    18.     }
    20.     public boolean onTouch(View v, MotionEvent event) {
    21.         int Action = event.getAction();
    22.         Geo.Geometry Geo = _oGeo.SetTouched((int)event.getX(), (int)event.getY());
    23.         switch (Action) {
    24.             case MotionEvent.ACTION_UP:
    25.                 if (Geo != null) {
    26.                     //Do Some Processing
    27.                 }
    28.         }
    30.         if (Geo != null) return true;
    31.         v.onTouchEvent(event);
    32.         v.setOnTouchListener(this);
    33.         return true;
    34.     }
    Ok so when the screen is touched, it will pass the coordinates into the Geo class and look for an object that contains those coordinates. If the touch was within the object, then I will do some processing and return true so the event is not passed down the the map. If the touch is not in the object then the event is passed down to the map as normal so the user can scroll and such.

    If they touch an object everything is fine, they can keep touching more and I keep getting the event. The problem is when its not on an object. I'll get the first ACTION_DOWN, ACTION_MOVE, and ACTION_UP but no more events after that hit the onTouch handler.

    What am I doing wrong?

  2. JoshKraker

    JoshKraker Member This Topic's Starter

    Dec 3, 2009
    Likes Received:
    For anyone that runs into the same problem, I was able to solve it by placing the onTouch code within the overlay.
  3. pjakesmith

    pjakesmith Guest

    It was an amazing article! A writer like you should be given credit for your dedication to work as your provide good quality articles with the good purpose. I like to read such articles for they tackle different issues in our society as well as different practicalities and knowledge that a certain person should or must know. I will keep reading your next post that will be an interesting article again as usual.

Share This Page