OSGEarth添加文字

目錄

一、類型轉換

二、編碼修改

三、文字添加

四、完整代碼 


本文主要介紹在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<osg::Group> earthLabel = new osg::Group;

	//字體屬性
	osgEarth::Style style;
	osgEarth::Symbology::TextSymbol * textStyle = style.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();
	//設置顏色
	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<osg::Group> earthLabel = new osg::Group;

	//字體屬性
	osgEarth::Style style;
	osgEarth::Symbology::TextSymbol * textStyle = style.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();
	//設置顏色
	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);
	}
	
}

 

發佈了277 篇原創文章 · 獲贊 222 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章