如何在 MKMapView 的路线中心显示 expectedTravelTime

How to display the expectedTravelTime in the center of a route on MKMapView

我想在 MKMapView 上指定路线的中心显示一个白色矩形,其中包含预期的行程时间。

这是我用来获取预计到达时间和路线中心点的代码,您可以使用预计到达时间坐标并创建自定义注释视图并将其添加到地图

-(void) showRouteBetweenSourceCoordinate:(CLLocationCoordinate2D) sourceCoordinate 目标坐标:(CLLocationCoordinate2D) destinationCoordinate onMapView:(MKMapView*) onMapView onCompletion:(void(^)(NSString *ETA,CLLocationCoordinate2D ETACoordinate,NSError *error)) onCompletion {

MKDirectionsRequest *request =
[[MKDirectionsRequest alloc] init];

MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoordinate addressDictionary:nil];
MKMapItem *sourceMapItem=[[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
request.source = sourceMapItem;

MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoordinate addressDictionary:nil];
MKMapItem *destinationMapItem=[[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
request.destination = destinationMapItem;

request.requestsAlternateRoutes = NO;
[request setTransportType:MKDirectionsTransportTypeAutomobile];

MKDirections *directions =
[[MKDirections alloc] initWithRequest:request];

[directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error) {
     if (error) {
         onCompletion(nil,CLLocationCoordinate2DMake(0, 0),error);
     } else {

         if (response.routes.count>0) {


             MKRoute *route=response.routes[0];

             MKMapPoint routePoint=route.polyline.points[route.polyline.pointCount/2]; // get the center point of the polyline

             [onMapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];

             CLLocationCoordinate2D etaCoordinate=MKCoordinateForMapPoint(routePoint);

             int etaInMins=(int)(route.expectedTravelTime/60);

             onCompletion([NSString stringWithFormat:@"%d min",etaInMins],etaCoordinate,nil);

         }
         else
             onCompletion(nil,CLLocationCoordinate2DMake(0, 0),nil);

     }
 }];

}