how to get the pointer/cursor location in android

匆匆过客 提交于 2019-12-30 11:35:11

问题


In Android I would like to get/return the cursor location (latitude and longitude) when I click anywhere on the screen.


回答1:


I don't know for what you want to get latitude and longitude but I am sure you can get the position of the coordinates X and Y when a user touch the screen.

Implement OnTouchListener in your Activity and set the Listener for your View using the setOnTouchListener() method.

Now you can override the onTouch() method in your Activity.

@Override
public boolean onTouch(View view, MotionEvent event) {
    int x = (int) event.getX();       
    int y = (int) event.getY();
    return false;
}

See I have written a sample snippet for you:

    public class OnTouchDemo extends Activity implements OnTouchListener {

    private TextView tView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tView = (TextView) findViewById(R.id.txtView1);
        tView.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        Log.v("OnTouchDemo", "X==" + String.valueOf(x) + " Y==" + String.valueOf(y));

        return false;
    }
}



回答2:


For a location x,y on your MapView ,

MapView    map    = (MapView) findViewById(R.id.youmapview);
Projection proj   = map.getProjection();
GeoPoint   latlon = proj.fromPixels(x, y);


来源:https://stackoverflow.com/questions/5126907/how-to-get-the-pointer-cursor-location-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!