Capture clicks on map

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Capture clicks on map

Postby immanueln2005 » Mon Jan 12, 2009 5:37 am

Hi,

I was looking towards capturing the latitude and longitude of any point I click on a map in my application.

setOnClickListener() / onTouchEvent() do not work. They are not fired when i click on the map.

onTap() dint really prove to be useful as i need to capture clicks ANYWHERE on the map.

I could not figure out how to override

Hope someone helps me out with this :)

Thanks,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Re: Capture clicks on map

Postby sridhar.s » Fri Jan 23, 2009 12:04 pm

immanueln2005 wrote:Hi,

I was looking towards capturing the latitude and longitude of any point I click on a map in my application.

setOnClickListener() / onTouchEvent() do not work. They are not fired when i click on the map.

onTap() dint really prove to be useful as i need to capture clicks ANYWHERE on the map.

I could not figure out how to override

Hope someone helps me out with this :)

Thanks,
Immanuel



hi

i am not sure. But you may try this code in your application

@Override
public void onCreate(Bundle icicl){
.............
.............
OnTouchListener myOnTouchListener=new OnTouchListener(){
public boolean onTouch(View mapView,MotionEvent m){
int action=m.getAction();
switch(action){
case MotionEvent.ACTION_UP:
int x=(int)m.getX();
int y=(int)m.getY();
double latspan=(myMapView.getLatitudeSpan()/1.0E6);
double lngspan=(myMapView.getLongitudeSpan()/1.0E6);
GeoPoint center=myMapView.getMapCenter();
double xpcent=(x/(double)myMapView.getWidth());
double ypcent=(y/(double)myMapView.getHeight());
double lat0=(center.getLatitudeE6()/1.0E6+(latspan/2.0));
double lng0=(center.getLongitudeE6()/1.0E6-(lngspan/2.0));
double lat=lat0-(ypcent*latspan);
double lng=lng0+(xpcent*lngspan);
Log.i("x=",""+x);
Log.i("y=",""+y);
GeoPoint newpoint=new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
// myMapView.getController().zoomOut();

Log.i("Map","Height"+myMapView.getHeight());
Log.i("Map","width"+myMapView.getWidth());
Log.i("Current","LatitudeSpane"+myMapView.getLatitudeSpan());
Log.i("Current","LongitudeSpan"+myMapView.getLongitudeSpan());
Log.i("center Longitude","is"+newpoint.getLongitudeE6());
Log.i("center","Latitude is"+newpoint.getLatitudeE6());
Log.i("new ","latitude "+lat);
Log.i("new","longitude "+lng);


Log.i("background","is"+myMapView.getBackground());
Log.i("Zoom in","works");

myMapView.invalidate();
mc.animateTo(newpoint);

mc.zoomIn();

break;

}
return true;
}
};
myMapView.setOnTouchListener(myOnTouchListener);
......
.....
}

Regards
Sridhar.S
sridhar.s
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 01, 2009 5:43 am

Postby immanueln2005 » Fri Jan 23, 2009 1:24 pm

Thanks a lot..

I'll just check out and let you know.

Regards,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby immanueln2005 » Tue Jan 27, 2009 5:57 am

Works wonderful... Just what i wanted...

Thanks sridhar...

Facing a new problem... not able to move the map using mouse..
My intention was to allow the user to choose/set a location.

I'll try finding it out... Do let me know if there's an easy solution..

Thanks again.. :D
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby MrSnowflake » Tue Jan 27, 2009 12:10 pm

Shouldn't you just call MapView.setClickable(true)?? Won't that fix both of your questions?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby immanueln2005 » Tue Jan 27, 2009 12:41 pm

MrSnowflake wrote:Shouldn't you just call MapView.setClickable(true)?? Won't that fix both of your questions?


It is set as clickable...

What actually happens in the code provided by sridhar is, he zoom's into the selected geopoint, and disabling my map controls.. :D
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Problem

Postby sridhar.s » Wed Jan 28, 2009 5:35 am

I am also facing the same problem. If you find out the answer please tell me.
sridhar.s
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 01, 2009 5:43 am

Postby redtalonn » Wed Jan 28, 2009 4:32 pm

How about creating an overlay and then using the onTap() method of it to get the GeoPoint?

Something like:
public class ClickGeoPoint extends Overlay{
BitmapDrawable bmp;
private Context context;

public ClickGeoPoint(Drawable d, Point p, Context _context) {
context = _context;
bmp = (BitmapDrawable) d;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
bmp.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
bmp.setAlpha(0);
bmp.draw(canvas);
}

@Override
public boolean onTap(GeoPoint p, MapView mapView) {
Toast.makeText(context, p.toString(), Toast.LENGTH_SHORT).show();
return true;
}

}
redtalonn
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jan 28, 2009 4:16 pm

onTap()

Postby sridhar.s » Thu Jan 29, 2009 5:18 am

redtalonn wrote:How about creating an overlay and then using the onTap() method of it to get the GeoPoint?

Something like:
public class ClickGeoPoint extends Overlay{
BitmapDrawable bmp;
private Context context;

public ClickGeoPoint(Drawable d, Point p, Context _context) {
context = _context;
bmp = (BitmapDrawable) d;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
bmp.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
bmp.setAlpha(0);
bmp.draw(canvas);
}

@Override
public boolean onTap(GeoPoint p, MapView mapView) {
Toast.makeText(context, p.toString(), Toast.LENGTH_SHORT).show();
return true;
}

}

Hi,
i am not fully clear about your doubt. Anyway you go througth the following link. it is very useful for you.

http://blog.pocketjourney.com/2008/03/1 ... oogle-map/

in this tutorial they show how to draw overlay and the onTap functionalities.

Regards
Sridhar.S
sridhar.s
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 01, 2009 5:43 am

Postby sridhar.s » Wed Feb 04, 2009 7:08 am

immanueln2005 wrote:Works wonderful... Just what i wanted...

Thanks sridhar...

Facing a new problem... not able to move the map using mouse..
My intention was to allow the user to choose/set a location.

I'll try finding it out... Do let me know if there's an easy solution..

Thanks again.. :D


Hi immanueln

in your code you just set the return value as false. Then you can move the map using mouse.

OnTouchListener myOnTouchListener=new OnTouchListener(){
public boolean onTouch(View mapView,MotionEvent m){
int action=m.getAction();
switch(action){
case MotionEvent.ACTION_UP:
........
........
break;

}
return false;
}
};
sridhar.s
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 01, 2009 5:43 am

Postby immanueln2005 » Wed Feb 04, 2009 10:01 am

Just what I wanted...

Thanks a lot for gettin back sridhar..
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby snivek » Sat Oct 17, 2009 2:04 pm

I know this thread is a few months old but I am trying to implement the same solution you are. When I return false at the end of onTouch the case for ACTION_UP never gets called. If I return false it does.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. OnTouchListener myOnTouchListener = new OnTouchListener() {
  3.  
  4.             public boolean onTouch(View v,MotionEvent m) {
  5.  
  6.                 int action = m.getAction();
  7.  
  8.                 //Log.i("TouchListener", "init");
  9.  
  10.                
  11.  
  12.                 switch(action){
  13.  
  14.                     case MotionEvent.ACTION_UP:
  15.  
  16.                         Log.i("TouchListener", "UP");
  17.  
  18.                         int x=(int)m.getX();
  19.  
  20.                         int y=(int)m.getY();
  21.  
  22.                         double latSpan = (mapView.getLatitudeSpan()/1.0E6);
  23.  
  24.                         double lngSpan = (mapView.getLongitudeSpan()/1.0E6);
  25.  
  26.                         GeoPoint center = mapView.getMapCenter();
  27.  
  28.                         double xpCent = (x/(double)mapView.getWidth());
  29.  
  30.                         double ypCent = (y/(double)mapView.getHeight());
  31.  
  32.                         double lat0 = (center.getLatitudeE6() / 1.0E6 + (latSpan/2.0));
  33.  
  34.                         double lng0 = (center.getLongitudeE6() / 1.0E6 - (lngSpan/2.0));
  35.  
  36.                         double lat = lat0 - (ypCent*latSpan);
  37.  
  38.                         double lng = lng0 + (xpCent*lngSpan);
  39.  
  40.                         GeoPoint newPoint = new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
  41.  
  42.                         mapView.getController().animateTo(newPoint);
  43.  
  44.                         Log.i("x=",""+x);
  45.  
  46.                         Log.i("y=",""+y);
  47.  
  48.                         break;
  49.  
  50. //                    case MotionEvent.ACTION_MOVE:
  51.  
  52. //                        Log.i("TouchListener", "MOVE");
  53.  
  54. //                        return true;
  55.  
  56. //                    case MotionEvent.ACTION_DOWN:
  57.  
  58. //                        Log.i("TouchListener", "DOWN");
  59.  
  60. //                        return true;
  61.  
  62. //                    
  63.  
  64. //                    case MotionEvent.ACTION_OUTSIDE:
  65.  
  66. //                        Log.i("TouchListener", "OUTSIDE");
  67.  
  68. //                        return true;
  69.  
  70. //                    default:
  71.  
  72. //                        Log.i("TouchListener", "fell through");
  73.  
  74. //                        return true;
  75.  
  76.                 }
  77.  
  78.                 return false;
  79.  
  80.             }
  81.  
  82.         };
  83.  
  84.         mapView.setOnTouchListener(myOnTouchListener);
  85.  
  86.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
snivek
Freshman
Freshman
 
Posts: 4
Joined: Wed Oct 07, 2009 5:25 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 3 guests