Reputation: 466
I want to detect the coordinates of a map are touched on the screen with a finger. The problem is when I touch the screen with a finger the coordinates are not displayed with a toast. I am using Google Maps v2. My code:
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
{
CrearLugar();
}
return super.onTouchEvent(event);
}
public void CrearLugar()
{
final GoogleMap mMap=null;
mMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Projection proj=mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"Lat: " + point.latitude + "\n" +
"Lng: " + point.longitude + "\n" +
"X: " + coord.x + " - Y: " + coord.y,
Toast.LENGTH_SHORT).show();
}
});
}
Everything is correct except when I touch the screen with a finger. When I do it, I want to display the coordinates of the place touched, but I can't watch anything, only the map.
What's wrong?
Thanks.
Upvotes: 0
Views: 299
Reputation: 6250
The click listener has to be set in any of the creators of the activity (onCreate, onResume, onPause, ...). Note that you are setting the listener to (listen) for changes. You can still give some use to the onTouch overriden method. It'll help to get the absolute coordinates where the user clicked in your screen, but it's completely independent from the map. (btw, what do you need the absoluta coordinates for?).
Try that:
@Override
public boolean onTouchEvent(MotionEvent event)
{
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"X: " + event.getX() + " - Y: " + event.getY(),
Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate view
// Inflate map view
mMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Projection proj=mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"Lat: " + point.latitude + "\n" +
"Lng: " + point.longitude + "\n",
Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 1