How to get the geographic location of a clicked position on a map?

I am doing mathematical calculations in the activity of an Android map, and I need to find the GeoPoint position that the user clicked on the map. I understand that I need to use the onTouch event handler. Here's what it looks like at the moment:

@Override
public boolean onTouch(View v, MotionEvent event) {

    int action = event.getAction();

    if(action == MotionEvent.ACTION_DOWN)
    {
        GeoPoint pGoal = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());

        DisplayInfoMessage("The screen has been touched!" + event.getX() + " AND " + event.getY() + "Latitude: "+ pGoal.getLatitudeE6());   // only for displaying test popup

    }
    super.onTouchEvent(event);

    return true;        

}  

However, he does not do what I want. Any help or hint is at least greatly appreciated!

+3
source share
1 answer

Actually this is already the answer:

@Override
    public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction();

        if(action == MotionEvent.ACTION_DOWN)
        {
            GeoPoint pGoal = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());

            DisplayInfoMessage("The screen has been touched!" + event.getX() + " AND " + event.getY() + "Latitude: "+ pGoal.getLatitudeE6());   // only for displaying test popup

        }
        super.onTouchEvent(event);

        return true;        

    }  
+1
source

Source: https://habr.com/ru/post/1760274/


All Articles