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开发者_StackOverflow社区: "+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);
}
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:
Projection proj = mapView.getProjection();
GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY());
String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG);
toast.show();
}
return super.dispatchTouchEvent(ev);
}
精彩评论