What is proper way to get ETA (estimated time arrival) from any location to my current location

前端 未结 2 443
陌清茗
陌清茗 2021-01-02 10:26

Would like to know what is proper way to get ETA (estimated time arrival) from any location to my current location, in consideration the following situation:

a. ex.

相关标签:
2条回答
  • 2021-01-02 10:52

    This worked for me using route.distance from M to the K's answer I was modifying code from this tutorial map directions tutorial

    (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;
    
    
                }
            }
    
        }];
    }
    
    0 讨论(0)
  • 2021-01-02 10:57

    I know this post is a bit old but in case someone is looking at the answer since iOS 7 Apple provide an API in MapKit in order to calculate all these info.

    Here is a snippet of how to use this API

        MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
        [request setSource:[MKMapItem mapItemForCurrentLocation]];
        [request setDestination:destination];
        [request setTransportType:MKDirectionsTransportTypeAutomobile];
        [request setRequestsAlternateRoutes:NO];
        MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
            if ( ! error && [response routes] > 0) {
                MKRoute *route = [[response routes] objectAtIndex:0];
                //route.distance  = The distance
                //route.expectedTravelTime = The ETA
            }
        }];
    
    0 讨论(0)
提交回复
热议问题