Android - 获取鼠标点击的像素的坐标

发布于 2024-12-20 04:48:08 字数 451 浏览 3 评论 0原文

我正在制作一个 Android 应用程序,它使用谷歌地图。一项功能是用户可以触摸地图上的一点并检索坐标。我在这里找到了一个很棒的教程 http://mobiforge.com/developing /story/using-google-maps-android?page=1 显示了如何使用 MotionEvent 实现此功能(您可以在教程中的“获取触摸的位置”下找到代码)。

我的问题是模拟器似乎不支持触摸事件,而且我没有 Android 设备可以随时测试它。所以我想要一种方法,它允许我用鼠标单击屏幕并获取所单击像素的坐标(以便将它们转换为地理坐标)。

从我到目前为止的研究来看,我找不到任何合适的东西,而且我也不否认是否有东西存在。有人可以帮忙吗?

I’m making an android app, which uses Google maps. One functionality is that the user may touch on a point of the map and retrieve the coordinates. I found a great tutorial here http://mobiforge.com/developing/story/using-google-maps-android?page=1 which shows how to implement this with a MotionEvent (you can find the code on the tutorial under "Getting the Location that was touched").

My problem is that touch events seem not to be supported by the emulator and I do not have an android device to test it anytime. So I would like to have a method, that allows me to click the screen with the mouse and get the coordinates of the pixel that was clicked (so to transform them to geocoordinates).

From my research so far I couldn’t find anything that fits and I do not no if something exists. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无言温柔 2024-12-27 04:48:08

这将帮助你:

oncreate代码:

   mapView = (MapView) findViewById(R.id.mapView);
            mapView.setBuiltInZoomControls(true);
            List<Overlay> mapOverlays = mapView.getOverlays();
            MapOverlay mapOverlay = new MapOverlay();

           mapOverlays.add(mapOverlay);

覆盖层的内部类将响应触摸(单击模拟器)

 class MapOverlay extends com.google.android.maps.Overlay
        {   

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub


             mc= mapView.getController();
            mc.animateTo(p);

            latitude=p.getLatitudeE6() / 1E6;
            longitude=p.getLongitudeE6() /1E6 ;
                    Toast.makeText(Activity.this, 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();
      return true;

        }
}

This will help u out:

oncreate code:

   mapView = (MapView) findViewById(R.id.mapView);
            mapView.setBuiltInZoomControls(true);
            List<Overlay> mapOverlays = mapView.getOverlays();
            MapOverlay mapOverlay = new MapOverlay();

           mapOverlays.add(mapOverlay);

inner class for overlay that will respond to touch (click on emulator)

 class MapOverlay extends com.google.android.maps.Overlay
        {   

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub


             mc= mapView.getController();
            mc.animateTo(p);

            latitude=p.getLatitudeE6() / 1E6;
            longitude=p.getLongitudeE6() /1E6 ;
                    Toast.makeText(Activity.this, 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();
      return true;

        }
}
聆听风音 2024-12-27 04:48:08

我想您会发现该特定教程也会响应鼠标点击。毕竟所有屏幕截图都是模拟器的。

关于作者编码风格的一点说明:

while

if (event.getAction() == 1)

will 的作用

if (event.getAction() == MotionEvent.ACTION_UP) 

是相同的,但更具描述性和更好的实践

I think you'll find that particular tutorial will respond to mouse clicks too. After all the screen shots are of an emulator.

Just one note on the author's coding style:

whilst

if (event.getAction() == 1)

will work

if (event.getAction() == MotionEvent.ACTION_UP) 

amounts to the same thing but is more descriptive and better practice

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文