如下图所示:
MouseIntersection.h
#ifndef MOUSEINTERSECTION_H
#define MOUSEINTERSECTION_H
#include <osgGA/GUIEventHandler>
#include <osgEarthUtil/Controls>
//经纬度标牌显示
class MouseIntersection : public osgGA::GUIEventHandler {
public:
MouseIntersection();
virtual ~MouseIntersection();
osgEarth::Util::Controls::LabelControl*getLabel() {
return _label;
}
private:
void buildLabelControls();
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)override;
private:
osgEarth::Util::Controls::LabelControl* _label;
};
#endif // MOUSEINTERSECTION_H
MouseIntersection.cpp
#include "MouseIntersection.h"
#include "EarthMapManager.h"
#include <osgEarthUtil/Controls>
#include <osg/Node>
#include <sstream>
MouseIntersection::MouseIntersection() {
buildLabelControls();
}
MouseIntersection::~MouseIntersection() {
}
void MouseIntersection::buildLabelControls() {
EarthMapMgr()->getRoot()->addChild(osgEarth::Util::Controls::ControlCanvas::get(EarthMapMgr()->getViewer()));
osgEarth::Util::Controls::ControlCanvas* canvas = osgEarth::Util::Controls::ControlCanvas::get(EarthMapMgr()->getViewer());
_label = new osgEarth::Util::Controls::LabelControl;
_label->setText("label test");
_label->setHorizAlign(osgEarth::Util::Controls::LabelControl::Alignment::ALIGN_LEFT);
_label->setVertAlign(osgEarth::Util::Controls::LabelControl::Alignment::ALIGN_BOTTOM);
//_label->setEncoding(osgText::String::Encoding::ENCODING_ASCII);
_label->setMargin(osgEarth::Util::Controls::LabelControl::Side::SIDE_BOTTOM, 10.0);
_label->setBackColor(osg::Vec4(0, 0, 0, 0.2));
canvas->addControl(_label);
// 添加中文字体
osg::ref_ptr<osgText::Text> t = new osgText::Text;
t->setFont("./Map/3D/fonts/simsun.ttc");
_label->setFont(t->getFont());
_label->setEncoding(osgText::String::Encoding::ENCODING_UTF8);
}
bool MouseIntersection::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE || ea.getEventType() == osgGA::GUIEventAdapter::DRAG) {
osgUtil::LineSegmentIntersector::Intersections intersections;
osg::NodePath np;
np.push_back(EarthMapMgr()->getMapNode());
if (EarthMapMgr()->getViewer()->computeIntersections(ea.getX(), ea.getY(), np, intersections)) {
if (intersections.size() >= 1) {
osgUtil::LineSegmentIntersector::Intersections::iterator it = intersections.begin();
osg::Vec3d point;
point = it->getWorldIntersectPoint();
double latitude = 0.0;
double longitude = 0.0;
double height = 0.0;
EarthMapMgr()->getMapNode()->getMapSRS()->getEllipsoid()
->convertXYZToLatLongHeight(point.x(), point.y(), point.z(), latitude, longitude, height);
QString context = QString::fromLocal8Bit("经度:") + QString::number(osg::RadiansToDegrees(longitude)) + ", "
+ QString::fromLocal8Bit("纬度:") + QString::number(osg::RadiansToDegrees(latitude)) + ", "
+ QString::fromLocal8Bit("海拔:") + QString::number(height);
_label->setText(context.toStdString());
}
}
}
return false;
}
调用:
osg::ref_ptr<MouseIntersection> mouseInter;
mouseInter = new MouseIntersection;
_viewer->addEventHandler(mouseInter);
_root->addChild(mouseInter->getLabel());
aaa