首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在运行时添加osgEarth特性?

如何在运行时添加osgEarth特性?
EN

Stack Overflow用户
提问于 2018-11-17 14:03:27
回答 1查看 1.1K关注 0票数 1

我正在使用osgEarth,虽然在.earth文件中添加功能非常容易,但我很难通过API在运行时做到这一点。我想让用户在地图/地球仪上绘制多边形,因此我需要能够根据用户输入动态定义几何图形和样式。

现在,我只需要一个静态实现来确定我需要做什么,但在我的生命中,我无法得到任何东西来显示。这是我的代码样本。我已经加载了一个.earth文件,它定义了MapNode,这正是我在这里使用的。

代码语言:javascript
复制
// Style
osgEarth::Symbology::Style shapeStyle;
shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>()->fill()->color() = osgEarth::Symbology::Color::Green;

// Geometry
osgEarth::Symbology::Polygon* polygon = new osgEarth::Symbology::Polygon();
polygon->push_back(0, 0);
polygon->push_back(0, 10);
polygon->push_back(10, 10);

// Feature
osgEarth::Features::Feature* feature = new osgEarth::Features::Feature(polygon, mapNode->getMapSRS(), shapeStyle);

// Node
osgEarth::Annotation::FeatureNode* featureNode = new osgEarth::Annotation::FeatureNode(mapNode, feature);
featureNode->setStyle(shapeStyle);
featureNode->init();

mapNode->addChild(featureNode);

这应该在地图中央画一个绿色的三角形,但我什么也没看到。假设我的多边形点是地理坐标(lon,lat),这是错误的吗?像这样在飞行中创造我的风格和几何是不对的吗?我做错了什么?

Update:这看起来在3D (地心图)地图上工作得很好,但在2D (投影)地图上却不起作用,这正是我所追求的。

EN

回答 1

Stack Overflow用户

发布于 2018-11-17 17:10:48

在浏览了一下之后,我偶然发现了SDK附带的osgearth_features示例,其中包括通过编程创建特性的示例。我遵循了样本中的模式,并想出了一些有用的东西。

代码语言:javascript
复制
// Style
osgEarth::Symbology::Style shapeStyle;
osgEarth::Symbology::PolygonSymbol* fillStyle = shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>();
fillStyle->fill()->color() = osgEarth::Symbology::Color::Green;
osgEarth::Symbology::LineSymbol* lineStyle = shapeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>();
lineStyle->stroke()->color() = osgEarth::Symbology::Color::Black;
lineStyle->stroke()->width() = 2.0f;

// Geometry
osgEarth::Symbology::Polygon* polygon = new osgEarth::Symbology::Polygon();
polygon->push_back(0, 0, 10000);
polygon->push_back(0, 10, 10000);
polygon->push_back(10, 10, 10000);

// Feature Options (references the geometry)
osgEarth::Drivers::OGRFeatureOptions featureOptions;
featureOptions.geometry() = polygon;

// Model Options (references the feature options and style)
osgEarth::Drivers::FeatureGeomModelOptions geomOptions;
geomOptions.featureOptions() = featureOptions;
geomOptions.styles() = new osgEarth::StyleSheet();
geomOptions.styles()->addStyle( shapeStyle );
geomOptions.enableLighting() = false;

// Model Layer Options (created using the model options)
osgEarth::ModelLayerOptions layerOptions("test polygon", geomOptions);
mapNode->getMap()->addModelLayer(new osgEarth::ModelLayer(layerOptions));

定义样式和几何图形与我之前所做的差不多(这次我添加了一行符号),但在本例中,我在地图中添加了一个ModelLayer。ModelLayer使用了一些模型选项,这些选项通过特性选项引用了我的样式和几何学。

我不知道这是否是最好的方法,也不知道它有多大的可伸缩性(我能反复这么做吗?),至少让我开始了,

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53351947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档