我想在MKMapView的指定路线的中心显示一个白色矩形,其预期行程时间。
答案 0 :(得分:0)
以下是我用来获取ETA& amp;路线的中心点,您可以使用ETACoordinate并创建自定义注释视图并将其添加到地图
- (void)showRouteBetweenSourceCoordinate:(CLLocationCoordinate2D)sourceCoordinate destinationCoordinate:(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);
}
}];
}