osgearth中placenode不用修改源码,最简单粗暴的方式达到显示中文的效果:
直接上代码:
void GraphicsView::addPlaceNode(string ss, double lon, double lat)
{
osgEarth::Symbology::Style pm;
pm.getOrCreate<osgEarth::Symbology::IconSymbol>()->url()->setLiteral(placeMarkPath);
pm.getOrCreate<osgEarth::Symbology::IconSymbol>()->declutter() = true;
pm.getOrCreate<osgEarth::Symbology::TextSymbol>()->font() = FontPath; //必须是支持中文的字体
pm.getOrCreate<osgEarth::Symbology::TextSymbol>()->halo() = osgEarth::Symbology::Color("#5f5f5f");
pm.getOrCreate<osgEarth::Symbology::TextSymbol>()->encoding() = osgEarth::Symbology::TextSymbol::ENCODING_UTF8; //编码方式改为utf8
pm.getOrCreate<osgEarth::Symbology::TextSymbol>()->size() = 25.0;
//两次转换即可显示中文
wchar_t* mss = multiByteToWideChar(ss);
std::string name = osgDB::convertUTF16toUTF8(mss);
osg::ref_ptr<osgEarth::Annotation::PlaceNode> placeNode = new osgEarth::Annotation::PlaceNode(m_pMapNode, osgEarth::GeoPoint(m_pMapSRS, lon, lat), name, pm);
placeNode->setName(name);
//installNodeHighlighter(placeNode);
osg::ref_ptr<osg::LOD> placeLOD = new osg::LOD;
placeLOD->addChild(placeNode, 0.0, equatorRadius*2);
if (nullptr == m_pPlaceNodeRoot)
{
m_pPlaceNodeRoot = new osg::Group;
m_pPlaceNodeRoot->setName("PlaceNodeRoot");
osg::Group* root = dynamic_cast<osg::Group*>(MapNode::get(m_pDBNode));
root->addChild(m_pPlaceNodeRoot);
}
m_pPlaceNodeRoot->addChild(placeLOD);
}
multiByteToWideChar函数如下:
wchar_t *multiByteToWideChar(const string& pKey)
{
const char* pCStrKey = pKey.c_str();
int pSize = MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, NULL, 0);
wchar_t *pWCStrKey = new wchar_t[pSize];
MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, pWCStrKey, pSize);
return pWCStrKey;
}
调用:
addPlaceNode("北京", 117.5, 39.38);
效果: