MapView, LongPress, GestureDetector, OnTouchListener

Quickly share your Android Code Snippets here...

MapView, LongPress, GestureDetector, OnTouchListener

Postby plusminus » Wed Oct 08, 2008 5:01 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected void applyMapViewLongPressListener() {
  2.                 final GestureDetector gd = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){
  3.                         @Override
  4.                         public void onLongPress(MotionEvent e) {
  5.                                 // handle longpress to Mapview...
  6.                         }                      
  7.                 });
  8.                 this.mMapView.setOnTouchListener(new OnTouchListener(){
  9.                         @Override
  10.                         public boolean onTouch(View v, MotionEvent ev) {
  11.                                 return gd.onTouchEvent(ev);
  12.                         }
  13.                 });
  14.         }
Parsed in 0.032 seconds, using GeSHi

Works at least with: SDK 1.0
Image | Android Development Community / Tutorials
User avatar
Site Admin
Site Admin
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany


Can't seem to make it work?

Postby insanomania911 » Tue Jun 09, 2009 4:58 pm

I tried your code and other codes as well , I can't seem to make this work.

All I need is to click on my map and have some sort of a listener trigger like a popup window,dialog or Toast giving the user the long/lat. It just doesn't work. I have my MapView created in my xml main layout fle where I set android:clickable="true" and android:enabled="true"

Also I have the following code:

class MapOverlay extends

public boolean onTouchEvent(MotionEvent event, MapView mapView)

System.out.println("reached here 2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//---when user lifts his finger---
if (event.getAction() == MotionEvent.ACTION_UP) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,

System.out.println("Touched this loc: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+p);
return false;


what am I doing wrong? because I am not getting anything.

Junior Developer
Junior Developer
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby insanomania911 » Sat Jun 13, 2009 11:13 pm

Junior Developer
Junior Developer
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby burujo » Sun Aug 16, 2009 6:24 pm

I'm not sure, but I think that you need to use the setOnTouchListener from mapView
Posts: 4
Joined: Thu Mar 26, 2009 1:32 am

Postby loudo » Mon Oct 26, 2009 3:13 pm


I have test this code but it seems to work only once. Do you know why ?

Posts: 5
Joined: Thu Jul 23, 2009 1:39 pm

Re: MapView, LongPress, GestureDetector, OnTouchListener

Postby Geo Gnome » Sun May 30, 2010 4:16 pm

It appears you have too many ! characters in your System.out.println() message. Try reducing the number of them and running it again.

In all seriousness, android seem to quietly die when things don't go the way it wants. You may try assigning all of your values to variables, and then making the call, that way you could put sysouts between each of the assignments to see where it's blowing up. Good -old fashioned style.

It would also help to make sure you assign the listener to the mapview, or the overlay (not sure which it gets assigned to).
Geo Gnome
Posts: 2
Joined: Sun Dec 20, 2009 12:58 pm


Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests