osgEarth的中文乱码问题解决
1.osg解决方法:
osgDB自带一个转换函数
std::string beiJingStr = "北京";
std::string str = osgDB::convertStringFromCurrentCodePageToUTF8(beiJingStr);
2.qt解决方法:
qt中可以使用QString::fromLocal8Bit或者QStringLiteral
QString beiJingStr = QString::fromLocal8Bit("北京");
//QString beiJingStr = QStringLiteral("北京");
std::string str = beiJingStr.toStdString();
3.重点来了
textstyle编码方式设置为utf8,不设置的话还是不能成功转换
textStyle->encoding() = osgEarth::TextSymbol::Encoding::ENCODING_UTF8;
4.添加地标代码
void addLabel()//添加地标
{
osgEarth::Style style;
osgEarth::TextSymbol* textStyle = style.getOrCreateSymbol<osgEarth::TextSymbol>();
//设置颜色
textStyle->fill()->color() = osg::Vec4f(1.0, 1.0, 1.0,0.5);
//设置边框
//textStyle->halo()->color()= osg::Vec4f(1.0, 1.0, 1.0, 1.0);
QString fontPath = QCoreApplication::applicationDirPath() + "./data/fonts/simkai.ttf";
textStyle->font()= fontPath.toStdString();
textStyle->size() = 20.0;
textStyle->encoding() = osgEarth::TextSymbol::Encoding::ENCODING_UTF8;
QString ChinaImagePath = QCoreApplication::applicationDirPath() + "./data/icons/China.jpg";
osg::Image* ChinaImage = osgDB::readImageFile(ChinaImagePath.toStdString());
ChinaImage->scaleImage(100, 50, 1);
osgEarth::GeoPoint beiJingpos(mapNode->getMapSRS(), 116.2, 39.56, 0.0);
//test 设置中文
/*
1.qt的方法 设置QString::fromLocal8Bit或者QStringLiteral
2.osg自带转换方法 osgDB::convertStringFromCurrentCodePageToUTF8()
*/
//QString beiJingStr = QString::fromLocal8Bit("北京");
std::string beiJingStr = "北京";
std::string str = osgDB::convertStringFromCurrentCodePageToUTF8(beiJingStr);
osgEarth::PlaceNode* pn = new osgEarth::PlaceNode(beiJingpos, str,style,ChinaImage);
root->addChild(pn);
}