Objective-C中MKDirections计算预计到达时间/距离返回0值求助
Hey there! Let's figure out why your MKDirectionsRequest is spitting out 0.0 for both expected travel time and distance. I’ve run into this exact issue before, so here are the most likely fixes to try:
You didn’t set both the start and end points for the request
The code snippet you shared only shows initializing anMKPlacemark, butMKDirectionsRequestrequires both asourceanddestination(asMKMapItemobjects) to calculate a route. Here’s how to fix that:// First, create your start point (e.g., user's current location) CLLocationCoordinate2D startCoord = userLocation.coordinate; // Replace with your actual start coordinate MKPlacemark *startPlacemark = [[MKPlacemark alloc] initWithCoordinate:startCoord addressDictionary:nil]; MKMapItem *startMapItem = [[MKMapItem alloc] initWithPlacemark:startPlacemark]; // Your destination placemark (from your geocoded address) MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(yourGeocodedLat, yourGeocodedLon) addressDictionary:nil]; MKMapItem *endMapItem = [[MKMapItem alloc] initWithPlacemark:endPlacemark]; // Assign both to your request MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init]; request.source = startMapItem; request.destination = endMapItem;You forgot to set a valid transport type
If you don’t specify atransportType, the request might not know how to calculate the route (or default to an unsupported type for your region). Add this line after initializing the request:// Choose the right type for your use case: Automobile, Walking, Transit, etc. request.transportType = MKDirectionsTransportTypeAutomobile;Note: Transit support depends on the region you’re working in—start with Automobile or Walking if you’re testing.
Your geocoded coordinates are invalid
Double-check that the coordinates you’re passing toMKPlacemarkare valid (not0.0, 0.0). Add a log before creating the placemark to verify:NSLog(@"Geocoded destination: %f, %f", yourGeocodedLat, yourGeocodedLon);If you see
0.0here, your geocoding step is failing—go back and debug that code first (make sure the address string is formatted correctly, and the geocoder is returning valid results).You’re not handling the directions response correctly
Make sure you’re properly fetching the route from the response in the completion handler. Here’s the full flow to get valid distance and time:MKDirections *directions = [[MKDirections alloc] initWithRequest:request]; [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) { if (error) { NSLog(@"Error calculating directions: %@", error.localizedDescription); return; } // Grab the first (usually optimal) route MKRoute *route = response.routes.firstObject; if (route) { NSLog(@"Distance: %.2f meters", route.distance); NSLog(@"Expected travel time: %.2f seconds", route.expectedTravelTime); } else { NSLog(@"No routes found between these two locations"); } }];If
routeis nil, that means no valid path exists for the chosen transport type (e.g., two locations separated by an ocean with Automobile selected).Missing location permissions
Ensure you’ve added the required location permission strings to yourInfo.plist:NSLocationWhenInUseUsageDescription(for when the app is active)NSLocationAlwaysAndWhenInUseUsageDescription(for background use)
Without these, the system might block location-related services likeMKDirections.
Start by verifying the first two points—those are the most common culprits! Let me know if any of these fix your issue.
内容的提问来源于stack exchange,提问作者Man

