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());
#include <osgGA/StateSetManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgEarth/MapNode>
#include <osgEarthUtil/EarthManipulator>
osgearth 原有漫游器EarthManipulator 不支持此功能,所以需要修改此漫游器代码,其中主要原理就是在缩放的同时也要改变当前漫游器中心焦点的位置,从而保持鼠标指定点的位置缩放是不改变。
核心代码修改部分
EarthManipulator  的头文件zoom方法添加参数view
         * Zoom the camera using delt...
感谢网友提出这个问题。大家有问题也可以在评论区提出,有问必有答。如果涉及商业需求,需要较完整/详尽的工程,可以联系作者133-2459-8743。
网友提出要做一个基于osgearth的导弹按轨迹飞行的程序,并放置一些地面站。我准备基于最新的oe版本3.1,分几个步骤把这个功能给做出来。节号命名为18.1, 18.2以示连贯。oe当前最新版本是3.1,说老实话不是很稳定。其提供的效果图如下:
本文集包括本节所有资源包括模型代码都在此下载,按节的序号有文件或文件夹:
注意: 务必使