计算到达目标C的预计到达时间和到达距离

时间:2018-05-03 07:51:42

标签: objective-c cllocation mkdirection

我正在获取地址,将它们作为字符串并使用地理编码我获得每个(纬度和经度)的坐标,我需要计算到达的估计到达时间和估计距离,并在一定半径内通知用户。 我正在使用此代码,但它不起作用,我只获得0.00000两个,任何帮助将不胜感激。感谢

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

0 个答案:

没有答案