前文链接:QGraphicsView实现简易地图11『指定层级-定位坐标』
提供地图平移与偏移功能。地图平移是指将地图的中心点更改为给定的点,即移动地图到指定位置。地图偏移是指将当前视口内的地图向上/下/左/右/进行微调,这里偏移视口宽/高的四分之一。
1、动态演示效果
2、平移与偏移代码
void WHMapView::panTo(const QPointF &geoCenter)
{m_geoCoord = GeoCoord(geoCenter.x(), geoCenter.y());// 经纬度坐标转场景坐标、视图定位到中心点QPointF offsetPos = QPointF(viewport()->width() / 2.0, viewport()->height() / 2.0);QPointF scenePos = MapUtility::sceneCoordFromGeoCoord(m_geoCoord, m_curLevel);horizontalScrollBar()->setValue(scenePos.x() - offsetPos.x());verticalScrollBar()->setValue(scenePos.y() - offsetPos.y());moveScene();emit geoCoordChanged(m_geoCoord);
}void WHMapView::panTop()
{double offsetValue = 4.0;QPointF scenePos = mapToScene(viewport()->width() / 2.0, viewport()->height() / offsetValue);m_geoCoord = MapUtility::geoCoordFromScene(scenePos, m_curLevel);verticalScrollBar()->setValue(verticalScrollBar()->value() + viewport()->height() / offsetValue);moveScene();emit geoCoordChanged(m_geoCoord);
}void WHMapView::panBottom()
{double offsetValue = 4.0;QPointF scenePos = mapToScene(viewport()->width() / 2.0, viewport()->height() / offsetValue);m_geoCoord = MapUtility::geoCoordFromScene(scenePos, m_curLevel);verticalScrollBar()->setValue(verticalScrollBar()->value() - viewport()->height() / offsetValue);moveScene();emit geoCoordChanged(m_geoCoord);
}void WHMapView::panLeft()
{double offsetValue = 4.0;QPointF scenePos = mapToScene(viewport()->width() / offsetValue, viewport()->height() / 2.0);m_geoCoord = MapUtility::geoCoordFromScene(scenePos, m_curLevel);horizontalScrollBar()->setValue(horizontalScrollBar()->value() + viewport()->width() / offsetValue);moveScene();emit geoCoordChanged(m_geoCoord);
}void WHMapView::panRight()
{int offsetValue = 4;QPointF scenePos = mapToScene(viewport()->width() / offsetValue, viewport()->height() / 2);m_geoCoord = MapUtility::geoCoordFromScene(scenePos, m_curLevel);horizontalScrollBar()->setValue(horizontalScrollBar()->value() - viewport()->width() / offsetValue);moveScene();emit geoCoordChanged(m_geoCoord);
}