You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 an MKPlacemark, but MKDirectionsRequest requires both a source and destination (as MKMapItem objects) 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 a transportType, 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 to MKPlacemark are valid (not 0.0, 0.0). Add a log before creating the placemark to verify:

    NSLog(@"Geocoded destination: %f, %f", yourGeocodedLat, yourGeocodedLon);
    

    If you see 0.0 here, 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 route is 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 your Info.plist:

    • NSLocationWhenInUseUsageDescription (for when the app is active)
    • NSLocationAlwaysAndWhenInUseUsageDescription (for background use)
      Without these, the system might block location-related services like MKDirections.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:38:59