赞
踩
已知点的经纬度信息,将点添加到地图中,并用指定的图标标注显示出来。思路很简单:首先创建一个图层,然后图层添加点位信息,最后设置该图层点位信息所关联的图标(注意这里图层所有的点位图标都是一样的)
- #include <qgis/core.h>
- #include <qgis/gui.h>
- #include <QApplication>
- #include <QIcon>
-
- int main(int argc, char* argv[])
- {
- // 初始化QGIS应用程序
- QgsApplication app(argc, argv, true);
-
- // 加载QGIS提供的GUI工具
- QgsGui::initResources();
-
- // 创建地图Canvas(地图画布)
- QgsMapCanvas canvas;
- canvas.setCanvasColor(Qt::white);
- canvas.enableAntiAliasing(true);
- canvas.setMinimumSize(800, 600);
-
- // 创建地图Layer(图层)
- QgsVectorLayer* layer = new QgsVectorLayer("Point?crs=EPSG:4326", "Points", "memory");
- if (!layer)
- {
- qDebug() << "Failed to create layer!";
- return -1;
- }
-
- // 创建Feature(要素)
- QgsFeature feature;
- QgsGeometry* geometry = new QgsGeometry();
-
- // 设置经纬度坐标,这里假设经度为x,纬度为y
- double x = 123.456;
- double y = 78.9;
- geometry->addPointXY(x, y);
- feature.setGeometry(geometry);
-
- // 将Feature添加到图层中
- QgsVectorLayerEditBuffer editBuffer(layer);
- editBuffer.addFeature(feature);
-
- // 获取点位图标
- QString iconPath = ":/icons/marker.png"; // 假设marker.png是资源文件中的图标
- QIcon icon(iconPath);
- QgsMarkerSymbolV2* markerSymbol = new QgsMarkerSymbolV2();
- markerSymbol->setIcon(icon);
- QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2(markerSymbol);
- layer->setRendererV2(renderer);
-
- // 将图层添加到地图Canvas中
- QgsMapLayerRegistry::instance()->addMapLayer(layer);
- canvas.setExtent(layer->extent());
- canvas.setLayers(QgsMapLayerRegistry::instance()->mapLayers().values());
-
- // 显示地图Canvas
- canvas.show();
-
- // 运行QGIS应用程序
- return app.exec();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。