OSGEarth显示经纬度标牌

如下图所示:
在这里插入图片描述
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

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值