这对我有用,使用从 M 到 K 的答案的 route.distance 我正在修改本教程地图方向教程中的代码
(IBAction)routeButtonPressed:(UIBarButtonItem *)sender {
MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:thePlacemark];
[directionsRequest setSource:[MKMapItem mapItemForCurrentLocation]];
[directionsRequest setDestination:[[MKMapItem alloc] initWithPlacemark:placemark]];
directionsRequest.transportType = MKDirectionsTransportTypeAutomobile;
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(@"Error %@", error.description);
} else {
routeDetails = response.routes.lastObject;
[self.mapView addOverlay:routeDetails.polyline];
self.destinationLabel.text = [placemark.addressDictionary objectForKey:@"Street"];
self.distanceLabel.text = [NSString stringWithFormat:@"%0.1f Miles", routeDetails.distance/1609.344];
self.etaLabel.text = [NSString stringWithFormat:@"%0.1f minutes",routeDetails.expectedTravelTime/60];
//self.transportLabel.text = [NSString stringWithFormat:@"%u" ,routeDetails.transportType];
self.allSteps = @"";
for (int i = 0; i < routeDetails.steps.count; i++) {
MKRouteStep *step = [routeDetails.steps objectAtIndex:i];
NSString *newStep = step.instructions;
self.allSteps = [self.allSteps stringByAppendingString:newStep];
self.allSteps = [self.allSteps stringByAppendingString:@"\n\n"];
self.steps.text = self.allSteps;
}
}
}];
}