首先初始化的时候设置好底图,并且设置好点的图层,以及线的图层
m_graphicsOverlay = new GraphicsOverlay(this); //新建图层
m_mapView->graphicsOverlays()->append(m_graphicsOverlay); //将图层添加到地图
testmarkerGraphic = new Graphic(this); //初始化点击marker
SimpleMarkerSymbol* markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("red"), 10.0f, this); //这个代表是点标记
testmarkerGraphic->setSymbol(markerSymbol);
m_graphicsOverlay->graphics()->append(testmarkerGraphic);
SimpleLineSymbol* pathSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("blue"), 5.0f /*width*/, this); //初始化line
m_pathGraphic = new Graphic(this);
m_pathGraphic->setSymbol(pathSymbol);
m_graphicsOverlay->graphics()->append(m_pathGraphic);
当鼠标点击的时候我在地图上绘制并且记录第一个点。我使用一个列表将他存起来,当这个列表大于2 的时候,我就可以进行绘制线了,不过首先需要将坐标转化为地图坐标,不知道的小伙伴可以查看我写的坐标转化。
QList <Point> m_List;
Point point = m_mapView->screenToLocation(event.x(), event.y()); //本地坐标转地图坐标
Point geometryWgs84 = GeometryEngine::project(point, SpatialReference::wgs84()); //坐标转化
m_List.append(geometryWgs84);
if (m_List.size() >= 2)
{
const QList<Point> points = { m_List[0], m_List[1] };
const Polyline polyline = pointsToPolyline(points);
constexpr double maxSegmentLength = 1.0;
const LinearUnit unitOfMeasurement(LinearUnitId::Kilometers);
constexpr GeodeticCurveType curveType = GeodeticCurveType::Geodesic;
const Geometry pathGeometry = GeometryEngine::densifyGeodetic(polyline, maxSegmentLength, unitOfMeasurement, curveType);
m_pathGraphic->setGeometry(pathGeometry);
qDebug() << "aaaaa::" << QString::number(GeometryEngine::lengthGeodetic(pathGeometry, unitOfMeasurement, curveType), 'f', 2); //测距
}
Polyline T3::pointsToPolyline(const QList<Point>& points)
{
PolylineBuilder polylineBuilder(SpatialReference(4326)); //将2个点传入
for (const Point& point : points)
polylineBuilder.addPoint(point);
return polylineBuilder.toPolyline();
}
效果图:
以及显示的公里数:
经过我的验证,没有任何问题
ヾ( ̄▽ ̄)ByeBye