Get coordinates when clicked anywhere in MapView
I just can't get this to work. I tried using below code with onTouchEventand and it doesn't work. If I return true at the end of the method, I get a toast with coordinates, but I cannot move the map, and if I return to false, I can move the map, but I cannot display the toast after the user clicks on the map. If I understood correctly, another onTap method is only used to click on overlays. Has anyone figured this 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);
}
+2
a source to share
1 answer
use dispatchTouchEvent () method. it works. why, since MapActivity inherits dispatchTouch event and not OnTouchEvent from activity class. check the 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);
}
+6
a source to share