目录
一、类型转换
二、编码修改
三、文字添加
四、完整代码
本文主要介绍在OSGEarth中实现在指定经纬度添加文字。
下列代码用于是实现将String类型的字符串转换为WString类型,才可被相应函数接收作为输入。
std::wstring String2WString(const std::string& s)
{
std::string strLocale = setlocale(LC_ALL, "");
const char* chSrc = s.c_str();
size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t* wchDest = new wchar_t[nDestSize];
wmemset(wchDest, 0, nDestSize);
mbstowcs(wchDest, chSrc, nDestSize);
std::wstring wstrResult = wchDest;
delete[]wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wstrResult;
}
下列代码用于实现将Unicode编码转换为UTF8编码,这样在OSGEarth中添加的中文才不会出错。
void unicodeToUTF8(const wstring &src, string& result)
{
int n = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0);
result.resize(n);
::WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0);
}
下列代码用于实现在指定经纬度添加文字,字体的属性可以根据需要进行修改,在添加英文时,可删除simkai.ttf字体属性设置;若添加中文,则不可省略simkai.ttf字体属性设置。
void addPositionName()
{
osg::ref_ptr earthLabel = new osg::Group;
//字体属性
osgEarth::Style style;
osgEarth::Symbology::TextSymbol * textStyle = style.getOrCreateSymbol();
//设置颜色
textStyle->fill()->color() = osg::Vec4f(1.0, 1.0, 1.0, 1.0);
//设置边框
textStyle->halo()->color() = osg::Vec4f(0.0, 0.0, 1.0, 1.0);
textStyle->font() = "simkai.ttf";
textStyle->size() = 13.0;
textStyle->encoding() = osgEarth::Symbology::TextSymbol::ENCODING_UTF8;
//读取文件
//std::fstream fin("./place/place.txt", std::ios::in);
string name[] = { "洗炼厂", "洗炼厂", "环保坝", "坝体", "生活区", "小阿希河" };//, "阿希金矿"};
double lon[] = { 81.617947, 81.623095, 81.623418, 81.622755, 81.614893, 81.622220 };//, 81.629543 };
double lat[] = { 44.237668, 44.245067 , 44.227526 , 44.230978 , 44.239895 , 44.225495 };//, 44.239629 };
for (int i = 0; i < 6; i ++)
{
//添加地名
const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
std::string _strWideName;
unicodeToUTF8(String2WString(name[i]), _strWideName);
//test
//MessageBox(NULL, "Hi", _strWideName.c_str(), MB_OK);
osgEarth::Annotation::PlaceNode *pn = new osgEarth::Annotation::PlaceNode(mapNode, osgEarth::GeoPoint(geoSRS, lon[i], lat[i]), NULL, _strWideName, style);
//pn->setScale(osg::Vec3(1, 1, 1));
earthLabel->addChild(pn);
}
}
std::wstring String2WString(const std::string& s)
{
std::string strLocale = setlocale(LC_ALL, "");
const char* chSrc = s.c_str();
size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t* wchDest = new wchar_t[nDestSize];
wmemset(wchDest, 0, nDestSize);
mbstowcs(wchDest, chSrc, nDestSize);
std::wstring wstrResult = wchDest;
delete[]wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wstrResult;
}
void unicodeToUTF8(const wstring &src, string& result)
{
int n = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0);
result.resize(n);
::WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0);
}
void addPositionName()
{
osg::ref_ptr earthLabel = new osg::Group;
//字体属性
osgEarth::Style style;
osgEarth::Symbology::TextSymbol * textStyle = style.getOrCreateSymbol();
//设置颜色
textStyle->fill()->color() = osg::Vec4f(1.0, 1.0, 1.0, 1.0);
//设置边框
textStyle->halo()->color() = osg::Vec4f(0.0, 0.0, 1.0, 1.0);
textStyle->font() = "simkai.ttf";
textStyle->size() = 13.0;
textStyle->encoding() = osgEarth::Symbology::TextSymbol::ENCODING_UTF8;
//读取文件
//std::fstream fin("./place/place.txt", std::ios::in);
string name[] = { "洗炼厂", "洗炼厂", "环保坝", "坝体", "生活区", "小阿希河" };//, "阿希金矿"};
double lon[] = { 81.617947, 81.623095, 81.623418, 81.622755, 81.614893, 81.622220 };//, 81.629543 };
double lat[] = { 44.237668, 44.245067 , 44.227526 , 44.230978 , 44.239895 , 44.225495 };//, 44.239629 };
for (int i = 0; i < 6; i ++)
{
//添加地名
const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
std::string _strWideName;
unicodeToUTF8(String2WString(name[i]), _strWideName);
//test
//MessageBox(NULL, "Hi", _strWideName.c_str(), MB_OK);
osgEarth::Annotation::PlaceNode *pn = new osgEarth::Annotation::PlaceNode(mapNode, osgEarth::GeoPoint(geoSRS, lon[i], lat[i]), NULL, _strWideName, style);
//pn->setScale(osg::Vec3(1, 1, 1));
earthLabel->addChild(pn);
}
}