qt和arcgis for qt在地图上做测距(画线和显示距离,单位km)

首先初始化的时候设置好底图,并且设置好点的图层,以及线的图层

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

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油小杜(接qt定制功能,单模块开发等)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值