QT的QGeoRoutingManager類的使用
阿新 • • 發佈:2020-12-12
詳細說明
QGeoRoutingManager類提供對地理路由操作的支援。
computeRoute()和updateRoute()方法函式QGeoRouteReply物件,它們管理這些操作並報告這些操作的結果以及可能發生的任何錯誤。
validateRoute()函式用於查詢遵循一組路點並匹配其他各種條件的一條或多條路線。 QGeoRouteRequest類用於指定此資訊。
如果supportsRouteUpdates()返回true,則QGeoRoutingManager支援基於位置更新來更新路線資訊。這將導致旅行時間和距離估計值得到更新,並且已經遍歷的所有QGeoRouteSegments將從路線中刪除。
可以使用QGeoServiceProvider :: routingManager()訪問QGeoRoutingManager的例項。
以下是QGeoRoutingManager和QGeoRouteRequests用法的一個小示例:
class MyRouteHandler : public QObject
{
Q_OBJECT
public:
MyRouteHandler(QGeoRoutingManager *routingManager,
const QGeoCoordinate & origin,
const QGeoCoordinate &destination) {
QGeoRouteRequest request(origin, destination);
// The request defaults to the fastest route by car, which is
// equivalent to:
// request.setTravelMode(QGeoRouteRequest::CarTravel);
// request.setRouteOptimization( QGeoRouteRequest::FastestRoute);
request.setAvoidFeatureTypes(QGeoRouteRequest::AvoidTolls);
request.setAvoidFeatureTypes(QGeoRouteRequest::AvoidMotorPoolLanes);
QGeoRouteRequest::AvoidFeaturesTypes avoidableFeatures = routingManager->supportedAvoidFeatureTypes();
if (!(avoidableFeatures & request.avoidFeatureTypes())) {
// ... inform the user that the routing manager does not
// provide support for avoiding tolls and/or motor pool lanes ...
return;
}
QGeoRouteReply *reply = routingManager->calculateRoute(request);
if (reply->isFinished()) {
if (reply->error() == QGeoRouteReply::NoError) {
routeCalculated(reply);
} else {
routeError(reply, reply->error(), reply->errorString());
}
return;
}
connect(routingManager,
SIGNAL(finished(QGeoRouteReply*)),
this,
SLOT(routeCalculated(QGeoRouteReply*)));
connect(routingManager,
SIGNAL(error(QGeoRouteReply*,QGeoRouteReply::Error,QString)),
this,
SLOT(routeError(QGeoRouteReply*,QGeoRouteReply::Error,QString)));
}
private slots:
void routeCalculated(QGeoRouteReply *reply)
{
// A route request can ask for several alternative routes ...
if (reply->routes().size() != 0) {
// ... but by default it will only get a single route
QGeoRoute route = reply->routes().at(0);
//... now we have to make use of the route ...
}
reply->deleteLater();
}
void routeError(QGeoRouteReply *reply, QGeoRouteReply:Error error, const QString &errorString)
{
// ... inform the user that an error has occurred ...
reply->deleteLater();
}
};