iOS 中两个位置之间的估计时间

Estimated Time Between two Locations in iOS

从 AnyLocation 到 MyCurrentLocation 获取 ETA(预计到达时间)的正确方法是什么?

我想,当点击任何注释时,我必须根据该注释和用户的当前位置在页脚上设置详细信息。

请看下图以便更好地理解。

我看到了这个 What is proper way to get ETA (estimated time arrival) from any location to my current location and this calculate time taken to cover a journey in Apple Maps 但是他们没有用这个解决我的问题。使用这个我总是得到 Null 响应。我把我的代码放在下面:-

double latitude = -0.075410;
double longitude = 51.512520;
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil] ;
MKMapItem *mapItemSource = [[MKMapItem alloc] initWithPlacemark:placemark];

double latitude1 = -0.132128;
double longitude1 = 51.464138;
MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude1, longitude1) addressDictionary:nil] ;
MKMapItem *mapItemDestination = [[MKMapItem alloc] initWithPlacemark:placemark1];

MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
[directionsRequest setSource:mapItemSource];
[directionsRequest setDestination:mapItemDestination];
directionsRequest.transportType = MKDirectionsTransportTypeAutomobile;
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];

[directions calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error %@", error.description);
    } else {
        NSLog(@"expectedTravelTime %f", response.expectedTravelTime);
    }
}];
//I am not sure why I am getting Null response into calculateETAWithCompletionHandler this method..

//Also tried following......
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error %@", error.description);
    } else {
        MKRoute * routeDetails = response.routes.lastObject;
        //[self.mapView addOverlay:routeDetails.polyline];
        NSLog(@"Street %@",[placemark.addressDictionary objectForKey:@"Street"]);
        NSLog(@"distance %@",[NSString stringWithFormat:@"%0.1f Miles", routeDetails.distance/1609.344]);
        NSLog(@"expectedTravelTime %@",[NSString stringWithFormat:@"%0.1f minutes",routeDetails.expectedTravelTime/60]);
    }
}];
//I am not sure why I am getting Null response into calculateDirectionsWithCompletionHandler this method..

我也看到了这个Is there any way to determine the driving time between two locations using Apple's Maps API? and this How to calculate time required to complete the path in between two locations?. But RightNow, I don't want to use Google Maps Directions API。因为我的客户不想使用它。如果 Google API 是免费的,那么我当然更喜欢它。

此外,我尝试了distanceFromLocation方法。但是,我认为,这个解决方案假设两点之间的直线距离几乎没有用。

是否有任何其他解决方案...?

如有任何帮助,我们将不胜感激。

你可以使用 google api 并且默认情况下还有一个方法

locA = [[CLLocation alloc] initWithLatitude:[[latsArray objectAtIndex:0] floatValue] longitude:[[longArray objectAtIndex:0]  floatValue]];
locB = [[CLLocation alloc] initWithLatitude:[[latsArray objectAtIndex:1] floatValue] longitude:[[longArray objectAtIndex:1]  floatValue]];

distance = [locA distanceFromLocation:locB] * 0.000621371;

和散步时间

 float time  = distance / 3.1; //divide by 3.1 for by walk 

试试这个方法。这里要传递源和目的地的经纬度信息。

NSString *strUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=%@", lat,  longi, userLat,  userLong, @"DRIVING"];
NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSMutableArray *arrDistance=[result objectForKey:@"routes"];
    if ([arrDistance count]==0) {
        NSLog(@"N.A.");
    }
    else{
        NSMutableArray *arrLeg=[[arrDistance objectAtIndex:0]objectForKey:@"legs"];
        NSMutableDictionary *dictleg=[arrLeg objectAtIndex:0];
        NSLog(@"%@",[NSString stringWithFormat:@"Estimated Time %@",[[dictleg   objectForKey:@"duration"] objectForKey:@"text"]]);
    }
}
else{
    NSLog(@"N.A.");
}

如果您想要计算点之间的行程时间,您首先需要指定您想要驾车还是步行路线,然后生成路线。路线请求的结果将包括行程时间估计。

您需要使用 MapKit、MKDirectionsRequestMKDirections 类 等。您还需要配置您的应用程序以请求 Apple 获得生成行车路线的许可。我在网上找到 this link 解释了如何计算行车路线。