如何实时获取鼠标的地理坐标

本文介绍如何在地图应用中实现鼠标悬浮时获取地理坐标的功能,通过注册MouseMove事件并利用地图控件的ScreenToMap方法转换屏幕坐标至地理坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在系统中,鼠标滑过就会获取到鼠标所在位置的地理坐标值。实现效果如下: 

这个效果的实现是基于地图上鼠标经过事件的。在给地图注册了MouseMove事件后,在该事件中添加如下代码即可实现。

在这段代码中,就是将鼠标经过点的位置信息(相对于屏幕坐标)转为对应参考系的地理坐标信息,即:ScreenToMap,这个方法是地图控件自带的方法。

private void myMap_MouseMove(object sender, MouseEventArgs e)
        {
            if (myMap.Extent != null)
            {
                System.Windows.Point screenPoint = e.GetPosition(myMap);

                ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = myMap.ScreenToMap(screenPoint);

                if (mapPoint != null)
                    MapCoordsTextBlock.Text = string.Format("平面坐标: X = {0}, Y = {1}",
                        Math.Round(mapPoint.X, 4).ToString(), Math.Round(mapPoint.Y, 4).ToString());
            }
        }


 

### 如何在 osgEarth 中获取鼠标点击的地理坐标 在 osgEarth 中,可以通过使用事件处理机制来捕获鼠标点击事件,并将屏幕坐标转换为地理坐标。以下是一个完整的实现示例,展示了如何通过鼠标点击获取地理坐标。 #### 示例代码 以下是实现该功能的完整代码: ```cpp #include <osgEarth/MapNode> #include <osgEarthUtil/EarthManipulator> #include <osgViewer/Viewer> #include <iostream> // 定义鼠标拾取函数 bool handleMouseEvent(osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osgEarth::MapNode* mapNode) { if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) { // 获取当前鼠标位置 int x = ea.getX(); int y = ea.getY(); // 将屏幕坐标转换为世界坐标 osg::Vec3d world; if (mapNode->getWorldUnderCursor(x, y, world)) { // 将世界坐标转换为地理坐标 osgEarth::GeoPoint geoPoint; if (mapNode->getMapSRS()->inverse(world, geoPoint)) { std::cout << "点击的地理坐标为: " << geoPoint(lonlat).x() << ", " << geoPoint(lonlat).y() << std::endl; return true; } } } return false; } int main(int argc, char** argv) { // 创建 viewer osgViewer::Viewer viewer; // 加载地图文件 osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(); osgEarth::Config conf; conf.fromFile("path_to_your_map_file.earth"); // 替换为你的地图文件路径 map->fromConfig(conf); // 创建 MapNode osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map); // 设置地球操作器 viewer.setCameraManipulator(new osgEarthUtil::EarthManipulator()); // 添加场景 viewer.setSceneData(mapNode); // 添加事件处理程序 viewer.addEventHandler(new osgGA::GUIEventHandler { bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override { return handleMouseEvent(const_cast<osgGA::GUIEventAdapter&>(ea), aa, mapNode); } }); // 运行 viewer return viewer.run(); } ``` #### 代码说明 1. **事件捕获**:通过 `handleMouseEvent` 函数捕获鼠标释放事件,并判断是否为左键释放[^1]。 2. **屏幕坐标到世界坐标的转换**:使用 `mapNode->getWorldUnderCursor` 方法将屏幕坐标转换为世界坐标[^2]。 3. **世界坐标地理坐标的转换**:通过 `mapNode->getMapSRS()->inverse` 方法将世界坐标转换为地理坐标[^3]。 4. **输出地理坐标**:将地理坐标以经度和纬度的形式输出到控制台。 #### 注意事项 - 地图文件需要包含高程数据,否则无法正确计算地理坐标的高度值[^4]。 - 确保地图文件路径正确,且地图文件中配置了有效的影像和高程数据[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值