開發環境:Qt Creator(Qt 5.14.2)+ ArcGIS Runtime 100.8
本文只包含實現特定功能所需的API和代碼片段,以及某些問題的解決方案,用於個人備忘,排版爆炸,還請見諒。
功能實現:
一. 資源管理
1.Qt加載svg矢量圖
使用QtSvg中的QSvgRenderer
1 QImage loadSvg(QString path = PLANEPATH){ 2 // 加載svg,返回QImage 3 QSvgRenderer* svgRender = new QSvgRenderer(); 4 svgRender->load(path); 5 QImage image(x, y, QImage::Format_ARGB32); 6 image.fill(Qt::transparent); 7 QPainter painter(&image); 8 svgRender->render(&painter); 9 return image; 10 }
2.ArcGIS加載tpk格式地圖
使用TileCache,ArcGISTiledLayer和BaseMap。
示例(需要引用ArcGIS對應的頭文件,頭文件名稱一般和類型名稱相同):
Mapload_Widgets::Mapload_Widgets(QApplication* application, QWidget* parent /*=nullptr*/): QMainWindow(parent), form(new Ui::Form){ // Create the Widget view m_mapView = new MapGraphicsView(this); // 加載 Tile Pack 地圖文件,路徑為宏定義 const QString worldMapLocation = QString(WORLDMAPPATH); const QString countryMapLocation = QString(COUNTRYMAPPATH); TileCache* worldTileCache = new TileCache(worldMapLocation, this); TileCache* countryTileCache = new TileCache(countryMapLocation, this); ArcGISTiledLayer* worldTiledLayer = new ArcGISTiledLayer(worldTileCache, this); ArcGISTiledLayer* countryTiledLayer = new ArcGISTiledLayer(countryTileCache, this); // 設置附加圖層透明度 countryTiledLayer->setOpacity(0.7F); // 實例化地圖,設置附加圖層 Basemap* basemap = new Basemap(worldTiledLayer, this); m_map = new Map(basemap, this); m_map->operationalLayers()->append(countryTiledLayer); // Set map to map view m_mapView->setMap(m_map); // set the mapView as the central widget setCentralWidget(m_mapView); // 隱藏界面下方的"Powered by Esri" m_mapView->setAttributionTextVisible(false); }
3.ArcGIS加載顯示自定義圖片
Graphic:使用PictureMarkerSymbol實例化Graphic,添加到GraphicsOverlay中
// 用於GeometryEngine::ellipseGeodesic方法的參數類,指定圖片標記的位置 GeodesicEllipseParameters ellipseParams(Point(X, Y, SpatialReference::wgs84()), 1, 1); PictureMarkerSymbol* pictureMarkerSymbol = new PictureMarkerSymbol(image, this); graphic = new Graphic(GeometryEngine::ellipseGeodesic(ellipseParams),pictureMarkerSymbol, this); overlay->graphics()->appendgraphic);
二. 動畫
1.Qt控件的動畫效果
放縮/位移:使用QPropertyAnimation
2.定時移動
可以使用QTimer
3.ArcGIS更改圖片標記的方向和位置
Graphic::setGeometry(),PictureMarkerSymbol::setAngle()
GeodesicEllipseParameters ellipseParams(Point(x, y, SpatialReference::wgs84()), 1, 1); graphic->setGeometry(GeometryEngine::ellipseGeodesic(ellipseParams)); pictureMarkerSymbol->setAngle(degree);
問題:
1.QWidget提升后樣式表失效(Qt設計)
解決方案:重寫父類QWidget的paintEvent
1 // 重寫paintEvent使stylesheet生效 2 void QScaleWidget::paintEvent(QPaintEvent *e){ 3 QStyleOption opt; 4 opt.init(this); 5 QPainter p(this); 6 style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); 7 QWidget::paintEvent(e); 8 }