0

I am getting the addresses, make them as string and using geocoding I get coordinates for each (latitude and longitude) and I need to calculate estimated arrival time and estimated distance to arrive and notify the user within a certain radius. I am using this code, but it is not working and I just get 0.00000 for both, any help would be appreciated. thanks

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


MKPlacemark *placemark3 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(coordination.latitude,coordination.longitude) addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:placemark3];


    // source and destination are the relevant MKMapItem's
    MKPlacemark *placemark2 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude) addressDictionary:nil];

    MKMapItem *source = [[MKMapItem alloc] initWithPlacemark:placemark2];
    request.source = source; // MKMapItem
    request.destination = destination;
    // Specify the transportation type
    request.transportType = MKDirectionsTransportTypeAutomobile;
    // If you're open to getting more than one route, requestsAlternateRoutes = YES; else requestsAlternateRoutes = NO;
    request.requestsAlternateRoutes = NO;
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        if (!error) {
            self.directionsResponse = response;
        }
    }];

    MKRoute *route = self.directionsResponse.routes[0];
    CLLocationDistance distance = route.distance; //distance in meter
    self.arrivalDistance.text =[NSString stringWithFormat:@"Distance to arrive in meters:\"%f\"",distance];
    NSTimeInterval seconds = route.expectedTravelTime; //ETA in Seconds
    self.arrivalTime.text=[NSString stringWithFormat:@"Minutes to arrive:\"%f\"", (seconds/60)];
Man
  • 9
  • 4
  • What did you do to debug this? Did you step and check the values of the variables? There is an extra `}}];`. – Willeke May 03 '18 at 11:22
  • I did, it goes through the lines, but I don't see a specific variable for distance and seconds there, I am not very good at debugging. and I did NSLog, and it just print 0.00000 for both. Also the extra }}]; is for the other part that I didn't copy, thanks for catching it though. sorry for the confusion – Man May 03 '18 at 17:15
  • 2
    It wouldn't surprise me at all that `calculateDirectionsWithCompletionHandler:` is asynchrone, and that if you put a log at `self.directionsResponse = response;`, it will print AFTER you do `MKRoute *route = self.directionsResponse.routes[0];`. – Larme May 03 '18 at 17:38
  • what @Larme said... also you might move the last 5 lines up into the completionHandler and see how that works out. Also put a breakpoint on if (!error) and step through the completion handler. – TyR May 03 '18 at 18:12
  • you guys are awesome, I moved the last 5 lines inside the if statement and now works, thanks soooo much! – Man May 03 '18 at 18:44

0 Answers0