Man
Man

Reputation: 9

calculating estimated time of arrival and distance to arrival in objective C

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)];

Upvotes: 0

Views: 417

Answers (0)

Related Questions